如何在 VBA 语言中表达“如果值不为空”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1983649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I express "if value is not empty" in the VBA language?
提问by excel34
How do I express the condition "if value is not empty" in the VBA language? Is it something like this?
如何用 VBA 语言表达条件“如果值不为空”?它是这样的吗?
"if value is not empty then..."
Edit/Delete Message
回答by Jon Crowell
Use Not IsEmpty().
使用Not IsEmpty().
For example:
例如:
Sub DoStuffIfNotEmpty()
If Not IsEmpty(ActiveCell.Value) Then
MsgBox "I'm not empty!"
End If
End Sub
回答by Patrick Honorez
It depends on what you want to test:
这取决于您要测试的内容:
- for a string, you can use
If strName = vbNullStringorIF strName = ""orLen(strName) = 0(last one being supposedly faster) - for an object, you can use
If myObject is Nothing - for a recordset field, you could use
If isnull(rs!myField) - for an Excel cell, you could use
If range("B3") = ""orIsEmpty(myRange)
- 对于字符串,您可以使用
If strName = vbNullString或IF strName = ""或Len(strName) = 0(据说最后一个更快) - 对于一个对象,你可以使用
If myObject is Nothing - 对于记录集字段,您可以使用
If isnull(rs!myField) - 对于 Excel 单元格,您可以使用
If range("B3") = ""或IsEmpty(myRange)
Extended discussion available here(for Access, but most of it works for Excel as well).
回答by alexphi
Try this:
尝试这个:
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
回答by Marcand
Why not just use the built-in Format() function?
为什么不直接使用内置的 Format() 函数?
Dim vTest As Variant
vTest = Empty ' or vTest = null or vTest = ""
If Format(vTest) = vbNullString Then
doSomethingWhenEmpty()
Else
doSomethingElse()
End If
Format() will catch empty variants as well as null ones and transforms them in strings. I use it for things like null/empty validations and to check if an item has been selected in a combobox.
Format() 将捕获空变量和空变量并将它们转换为字符串。我将它用于诸如空/空验证之类的事情,并检查是否在组合框中选择了一个项目。
回答by Anthony
I am not sure if this is what you are looking for
我不确定这是否是您要找的
if var<>"" then
dosomething
or
或者
if isempty(thisworkbook.sheets("sheet1").range("a1").value)= false then
the ISEMPTY function can be used as well
也可以使用 ISEMPTY 函数
回答by Todd Main
Alexphi's suggestion is good. You can also hard code this by first creating a variable as a Variantand then assigning it to Empty. Then do an if/then with to possibly fill it. If it gets filled, it's not empty, if it doesn't, it remains empty. You check this then with IsEmpty.
Alexphi 的建议很好。您也可以通过首先创建一个变量作为 aVariant然后将其分配给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

