vba 为什么 If Not IsEmpty 不过滤掉空字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10048855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why doesn't If Not IsEmpty filter out empty strings?
提问by Kyle
What is wrong with my If
condition?
我的If
病情有什么问题?
If Not IsEmpty(Wrkgps_L3) And Not IsEmpty(Wrkgps_L4) Then
Wrkgps_L3L4 = Wrkgps_L3 & "," & Wrkgps_L4
End If
The Not
condition doesn't seem to work. The code within the If
statement gets executed even when both Wrkgps_L3
and Wrkgps_L4
are empty strings.
该Not
条件似乎不起作用。If
即使Wrkgps_L3
和Wrkgps_L4
都是空字符串,语句中的代码也会被执行。
Update:
更新:
Wrkgps_L3
and Wrkgps_L4
are variables that contain results returned from a function. I noticed that IsEmpty(Wrkgps_L3) = False
even though Wrkgps_L3 = ""
. I had to rewrite my code to
Wrkgps_L3
和Wrkgps_L4
是包含从函数返回的结果的变量。我注意到IsEmpty(Wrkgps_L3) = False
即使Wrkgps_L3 = ""
. 我不得不重写我的代码
If (Wrkgps_L3 <> "") And (Wrkgps_L4 <> "") Then
In any case, I am still intrigued to know why IsEmpty
doesn't work on variables with ""
?
无论如何,我仍然很想知道为什么IsEmpty
不能使用""
?
回答by Michael Liu
In Visual Basic, Empty
and ""
(an empty string) are two different things. Empty
is the uninitialized state of a Variant
variable, and IsEmpty
tests whether a Variant
variable has the Empty
value:
在 Visual Basic 中,Empty
和""
(空字符串)是两个不同的东西。Empty
是Variant
变量的未初始化状态,并IsEmpty
测试Variant
变量是否具有Empty
值:
Dim x As Variant
If IsEmpty(x) Then
Debug.Print "x is empty"
End If
As you've observed, you must compare against ""
when checking whether a String
variable contains an empty string.
正如您所观察到的,您必须""
在检查String
变量是否包含空字符串时进行比较。
回答by Steve Rindsberg
If the variables are strings, you could 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