代码之家  ›  专栏  ›  技术社区  ›  Michael

无法检查范围是否为空

vba
  •  0
  • Michael  · 技术社区  · 6 年前

    Excel 2007版

    我卡住了。

    Private Sub check_status_correctness(a_row)
    
        Dim status_col_rng As Range
        Dim status_val As String
        Dim found_status As Range
    
        Set status_col_rng = Sheets("Settings").Columns(2)
        status_val = Sheets(MAIN_SHEET).Cells(a_row, STATUS_COL)
        Set found_status = status_col_rng.Find(status_val, LookIn:=xlValues, LookAt:=xlWhole)
    
        If found_status Is Nothing Then
            Call announce_error(a_row, STATUS_COL)
        End If
    End Sub
    

    发现状态在断点处不算什么。如果我仔细检查调试器中的found_状态,它似乎什么都不是。但就在那一刻发现你的身份是没有给我虚假的。

    你能帮我理解怎样才能一无所获吗?

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  1
  •   FunThomas    6 年前

    你的支票 Nothing 是正确的。如图所示,对象变量 found_status

    Dim r As Range  ' r is not assigned, that means it is nothing
    If r Is Nothing Then
        Debug.Print "r is Nothing"
    End If
    

    要检查是否分配了对象变量,请执行以下操作:

    Set r = ActiveSheet.Range("A1")
    If Not r Is Nothing Then
        Debug.Print "r is assigned"
    End If