我试图检测是否设置了整数,如果没有,则跳过循环中的大部分代码(使用if语句)。这就是我所拥有的。
Do While hws.Cells(r, 9).Value <> ""
On Error Resume Next
ar = Null
ar = aws.Range("A:A").Find(hws.Cells(r, 2).Value).Row
If Not IsNull(ar) Then
'work with ar'
End If
r = r + 1
Loop
然而,当我运行它时,ar = Null
有问题。它说“无效使用null”。
答案 0 :(得分:7)
定义为Integer的变量在VBA中不能为Null。你必须找到另一种方法来做你想要的。例如,使用不同的数据类型或使用幻数来表示null(例如-1)。
在您的示例代码中,将为ar分配一个Long值(Range.Row为Long)或者它将引发错误。
答案 1 :(得分:1)
只使用变体和isempty:
Dim i
If IsEmpty(i) Then MsgBox "IsEmpty"
i = 10
If IsEmpty(i) Then
MsgBox "IsEmpty"
Else
MsgBox "not Empty"
End If
i = Empty
If IsEmpty(i) Then MsgBox "IsEmpty"
'a kind of Nullable behaviour you only can get with a variant
'do you have integer?
Dim j as Integer
j = 0
If j = 0 Then MsgBox "j is 0"
答案 2 :(得分:0)
查找返回范围:
Dim rf As Range
With aws.Range("A:A")
Set rf = .Find(hws.Cells(r, 2).Value)
If Not rf Is Nothing Then
Debug.Print "Found : " & rf.Address
End If
End With
- http://msdn.microsoft.com/en-us/library/aa195730(office.11).aspx