测试字符串是否为空 [英] Testing if a string is null
问题描述
我是 VBA 新手,还没有完全习惯语法,所以如果我的问题听起来很愚蠢,我很抱歉.
I am pretty new in VBA and I have not yet got used to the syntax completely, so I'm sorry if my question sounds stupid.
我正在 Word 2010 中使用 RequisitePro40 和 VBA 7.0.在我的一个模块中,我有以下循环和 If 条件:
I am working with RequisitePro40 and VBA 7.0 in Word 2010. In one of my modules I have the following loop and If conditions:
Dim rqRequirements As ReqPro40.Requirements
Dim rqRequirement As ReqPro40.Requirement
Const eAttrValueLookup_Label = 4
Dim a As Integer
...
For Each vReqKey In rqRequirements
Set rqRequirement = rqRequirements(vReqKey)
If rqRequirement.AttrValue("MyAttreName", eAttrValueLookup_Label).text <> Null Then
a = 1
End If
If rqRequirement.AttrValue("MyAttreName", eAttrValueLookup_Label).text = Null Then
a = 2
End If
Next
在循环的每次迭代中,a = 1 和 a = 2 都被执行!!
In each iteration of the loop, both a = 1 and a = 2 are executed!!
基于这个,相等和不等运算符是="和<>".因此,我希望 a = 1 或 a = 2 对字符串执行.我的语法有问题吗?还是 ReqPro 相关的问题?
Based on This, the equality and inequality operators are "=" and "<>". Therefore I would expect that either a = 1 or a = 2 execute for a string. Is there something wrong with my syntax? Or is it a ReqPro related Problem?
我也尝试使用Is"和IsNot"运算符,但它们导致编译器错误:类型不匹配
I also tried using "Is" and "IsNot" operators but they result in Compiler error: Type mismatch
有人可以帮我吗?
更新:实际目标是看看
rqRequirement.AttrValue("MyAttreName", eAttrValueLookup_Label).text
rqRequirement.AttrValue("MyAttreName", eAttrValueLookup_Label).text
是否为 Null.我添加了第二个 if 以表明该语句在某种程度上没有按照我期望的方式工作.
is Null or not. I added the second if to show the problem that the statement is somehow not working the way I expect it to work.
将Null"替换为vbNullString"没有做任何改变.
Replacing "Null" to "vbNullString" did not make any changes.
我也按照@Slai 的建议尝试了 IsNull 函数.结果几乎相同:
I also tried the IsNull function as @Slai suggested. the result is pretty much the same:
If IsNull(rqRequirement.AttrValue(att, eAttrValueLookup_Label).text) Then
a = 3
End If
If Not IsNull(rqRequirement.AttrValue(att, eAttrValueLookup_Label).text) Then
a = 4
End If
a = 3 和 a = 4 两个语句都为真并被执行.
Both statements a = 3 and a = 4 are true and executed.
推荐答案
VBA 不支持测试字符串是否为Null".VBA 不像 .NET 语言或 JavaScript(例如).基本变量类型都有一个默认值,从声明变量的那一刻起,字符串的长度为零 (""
) - 它没有未实例化的状态.您还可以测试 vbNullString.
VBA doesn't support testing whether a string is "Null". VBA isn't like a .NET language or JavaScript (for example). The basic variable types all have a default value, a String is of zero length (""
) from the moment the variable is declared - it has no uninstantiated state. You can also test for vbNullString.
如果你测试
Dim s as String
Debug.Print s = Null, s <> Null, s = "", s = "a", IsNull(s), s = vbNullString
回报是
Null Null True False False True
因此,如果您要测试是否已将任何内容分配给 String 变量,那么您唯一可以做的就是:
So if you're trying to test whether anything has been assigned to a String variable the only things you can do are:
Debug.Print Len(s), s = "", Len(s) = 0, s = vbNullString
哪个返回
0 True True True
请注意,这些可能性中最慢的是 s = ""
,尽管它看起来最容易记住.
Note that the slowest of these possibilities is s = ""
, even though it seems the simplest to remember.
这篇关于测试字符串是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!