1

我正在尝试检测是否设置了整数,如果没有,则跳过循环中的大部分代码(使用 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”。

4

3 回答 3

9

定义为整数的变量在 VBA 中不能为 Null。你将不得不找到另一种方法来做你想做的事。例如使用不同的数据类型或使用幻数来指示空值(例如-1)。

在您的示例代码中,要么 ar 将被分配一个 Long 值(Range.Row 是 Long),要么它会引发错误。

于 2010-10-26T12:17:14.727 回答
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"
于 2010-10-26T12:05:19.393 回答
0

Find 返回一个范围:

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

于 2010-10-26T12:33:51.410 回答