无法检查范围是否为空

时间:2018-08-20 08:55:50

标签: vba

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_status在断头台上一无所获。如果我在调试器中仔细检查found_status,似乎什么都没有。但是就在这时,found_status没什么让我错了。

您能帮助我了解如何一事无成吗?

enter image description here

1 个答案:

答案 0 :(得分:1)

您对Nothing的检查是正确的。如图所示,对象变量found_status not 没什么(或换句话说,已分配给Range)

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