我在VBA中很新,我还没有完全习惯语法,所以如果我的问题听起来很愚蠢,我很抱歉。
我在Word 2010中使用RequisitePro40和VBA 7.0。在我的一个模块中,我有以下循环和If条件:
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!
基于This,等式和不等式运算符是“=”和“<>”。因此,我希望字符串执行a = 1或a = 2。我的语法有问题吗?或者它是ReqPro相关问题?
我也尝试使用“Is”和“IsNot”运算符,但它们导致编译器错误:类型不匹配
有人可以帮我弄这个吗?
更新:实际目标是看是否
rqRequirement.AttrValue(“MyAttreName”,eAttrValueLookup_Label).text
是否为空。我添加了第二个if,以显示该语句以某种方式不按预期的方式工作的问题。
将“Null”替换为“vbNullString”没有做任何更改。
我也尝试过@Slai建议的IsNull函数。结果几乎相同:
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都为真并执行。
VBA不支持测试字符串是否为“Null”。 VBA不像.NET语言或JavaScript(例如)。基本变量类型都有一个默认值,从声明变量的那一刻起,String的长度为零(""
) - 它没有未实例化的状态。您还可以测试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变量,那么您可以做的唯一事情是:
Debug.Print Len(s), s = "", Len(s) = 0, s = vbNullString
哪个回报
0 True True True
请注意,这些可能性中最慢的是s = ""
,尽管它似乎最容易记住。
正如其他人所说,你想要测试字符串的null版本vbNullString,而不是特别针对Null
。除此之外,您还需要确保您的对象本身不为null。例如:
Dim rqRequirements As ReqPro40.Requirements
Dim rqRequirement As ReqPro40.Requirement
Const eAttrValueLookup_Label = 4
Dim a As Long ' Avoid Integer since it has a strong habit of causing overflow errors.
...
For Each vReqKey In rqRequirements
Set rqRequirement = rqRequirements(vReqKey)
If Not rqRequirement Is Nothing Then
If rqRequirement.AttrValue("MyAttreName", eAttrValueLookup_Label).text <> vbNullString Then
a = 1
End If
If rqRequirement.AttrValue("MyAttreName", eAttrValueLookup_Label).text = vbNullString Then
a = 2
End If
End If
Next
现在,我之前没有使用过这个特定的对象类型,但我相当肯定AttrValue("MyAttreName", eAttrValueLookup_Label)
正在返回某种对象。如果是这种情况,则首选以下模式:
Dim rqRequirements As ReqPro40.Requirements
Dim rqRequirement As ReqPro40.Requirement
Const eAttrValueLookup_Label = 4
Dim a As Long ' Avoid Integer since it has a strong habit of causing overflow errors.
...
For Each vReqKey In rqRequirements
Set rqRequirement = rqRequirements(vReqKey)
If Not rqRequirement Is Nothing Then
Dim Attribute as Object ' Or whatever type it should be
Set Attribute = rq.Requirement.AttrValue("MyAttreName", eAttrValueLookup)
If Not Attribute is Nothing Then
If Attribute.text <> Null Then
a = 1
End If
If Attribute.text = Null Then
a = 2
End If
End If
End If
Next
通过这种方式,如果我们实际上设置了text
,我们只会调用Attribute
的Attribute
属性。这避免了424次错误。
最后,如果你想弄清楚导致两个if运行的代码中发生了什么,那么执行以下操作:
Debug.Print "Attribute Text: ", Attribute.Text
这将允许您查看代码所看到的内容。您也可以考虑使用断点。
1)我认为你可以使用vbNullString来测试空字符串。否则,如果实际字符串值使用“Null”。
2)确保a声明为long
If rqRequirement.AttrValue("MyAttreName", eAttrValueLookup_Label).text <> vbNullString Then
a = 1
End If
If rqRequirement.AttrValue("MyAttreName", eAttrValueLookup_Label).text = vbNullString Then
a = 2
Else
a = 3
End If
为确保互相排斥,请仅提出一次问题。
a = IIf(rqRequirement.AttrValue("MyAttreName", eAttrValueLookup_Label).text = vbNullString , 2, 1)
您还可以使用If-Then-Else
构造,特别是如果您要同时执行其他操作。
上面的代码示例假设~.text
调用是正确的。