Why doesn't If Is IsEmpty filter out blank lines?

What happened to my If condition?

 If Not IsEmpty(Wrkgps_L3) And Not IsEmpty(Wrkgps_L4) Then Wrkgps_L3L4 = Wrkgps_L3 & "," & Wrkgps_L4 End If 

The Not condition does not work. The code inside the If statement runs even if both Wrkgps_L3 and Wrkgps_L4 are empty lines.

Update:

Wrkgps_L3 and Wrkgps_L4 are variables that contain the results returned by the function. I noticed that IsEmpty(Wrkgps_L3) = False , although Wrkgps_L3 = "" . I had to rewrite my code on

 If (Wrkgps_L3 <> "") And (Wrkgps_L4 <> "") Then 

Anyway, I'm still intrigued to know why IsEmpty doesn't work with variables with "" ?

+6
source share
2 answers

In Visual Basic Empty and "" (empty string) there are two different things. Empty is the uninitialized state of the Variant variable, and IsEmpty checks if the Variant variable is set to Empty :

 Dim x As Variant If IsEmpty(x) Then Debug.Print "x is empty" End If 

As you noticed, you should compare with "" , checking if the String variable contains an empty string.

+8
source

If the variables are strings, you can also:

 If Len(Wrkgps_L3) + Len(Wrkgps_L4) = 0 Then ' They're both empty string variables Else ' One or the other contains at least one character End If 
+1
source

Source: https://habr.com/ru/post/912634/


All Articles