If Len(vValue & vbNullString) > 0 Then
' we have a non-Null and non-empty String value
doSomething()
Else
' We have a Null or empty string value
doSomethingElse()
End If
Alexphi 的建议很好。您还可以通过先创建一个变量作为 Variant,然后将其分配给 Empty来硬编码它。然后执行 if/Then with 来可能填充它。如果它被填满,它就不是空的,如果它不被填满,它就是空的。然后用 IsEmpty检查这个。
Sub TestforEmpty()
Dim dt As Variant
dt = Empty
Dim today As Date
today = Date
If today = Date Then
dt = today
End If
If IsEmpty(dt) Then
MsgBox "It not is today"
Else
MsgBox "It is today"
End If
End Sub
Dim vTest As Variant
vTest = Empty ' or vTest = null or vTest = ""
If Format(vTest) = vbNullString Then
doSomethingWhenEmpty()
Else
doSomethingElse()
End If
Format ()将捕获空变量和空变量,并将它们转换为字符串。
我使用它来进行 null/null 验证,以及检查组合框中是否选择了某个项。
Sub fillEmptyCells()
Dim r As Range
Set r = Selection
For i = 1 To r.Rows.Count
For j = 1 To r.Columns.Count
If Cells(i, j).Value = "" Then
Cells(i, j).Select
Cells(i, j).Value = InputBox( _
"set value of cell at column " & Cells(1, j).Value & _
" and row " & Cells(i, 1))
End If
Next j
Next i
End Sub