如果变量有值,如何检查?

Gul*_*edy 4 excel vba excel-vba

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

如何检查它是否有值?

我试过这些:

If variable_name.Value = Empty then ....

If variable_name.Value = " " then ...
Run Code Online (Sandbox Code Playgroud)

但是,当变量包含文本,数字或空格等数据时,这些只会很好.

任何的想法?

use*_*963 11

取决于您正在测试的内容.

范围对象或单元格值?

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
Run Code Online (Sandbox Code Playgroud)