5

我有一个变量,其中包含用户选择的范围值。

如何检查它是否有值?

我试过这些:

If variable_name.Value = Empty then ....

If variable_name.Value = " " then ...

但这些只有在变量包含文本、数字或空格等数据时才有用。

任何的想法?

4

1 回答 1

12

取决于你在测试什么。

范围对象还是单元格值?

Sub test()

Dim rngObject As Range
Dim value As Variant

    Set rngObject = Sheet1.Range("A1:D5")

    If Not rngObject Is Nothing Then
    'If not nothing then run this code
    End If

    value = rngObject.Cells(1, 1).value
    If Not IsEmpty(value) Then
    'if Not empty then run this code
    End If

    If value <> vbNullString Then
    'if value is not nullstring then run this code
    End If


End Sub
于 2012-08-24T13:21:04.197 回答