我尝试,以检测是否是一个整数被设定,并且如果不是,请跳过大多数代码的循环(使用,如果声明).这里是什么我有这么。

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"。

有帮助吗?

解决方案

找回一个范围:

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(办公室。11条)。aspx

其他提示

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

在您的示例代码,无论是AR将被分配一个长值(Range.Row是一个长整数),或者它会引发错误。

只使用一个变体和的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"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top