代码之家  ›  专栏  ›  技术社区  ›  Arlen Beiler

如何在Excel VBA中将整数设为空?

  •  1
  • Arlen Beiler  · 技术社区  · 14 年前

    我试图检测是否设置了整数,如果没有,则跳过循环中的大部分代码(使用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 有问题。它说“无效使用空值”。

    3 回复  |  直到 14 年前
        1
  •  0
  •   Fionnuala    14 年前

    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

        2
  •  7
  •   Foole    14 年前

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

    在示例代码中,要么ar被分配一个长值(range.row是一个长值),要么它将抛出一个错误。

        3
  •  1
  •   OlimilOops    14 年前

    只需使用一个变体并满足以下条件:

    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"