如果检查变量是否有值, [英] How to check a variable if it have a value or not?
本文介绍了如果检查变量是否有值,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个变量,其中包含用户选择的范围值。
如何检查是否有值或者不是?
How can I check if it have a value or not?
我已经尝试过这些:
If variable_name.Value = Empty then ....
If variable_name.Value = " " then ...
但是,当变量包含文本或数字或空格等数据时,这些只有很好。
But these are only good when the variable contains data like text or numbers or whitespace.
任何想法?
推荐答案
取决于您正在测试的内容。
Depends what you are testing.
范围对象或单元格值?
A range object or a cell value?
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
这篇关于如果检查变量是否有值,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文