How do I express "if value is not empty" in the VBA language?
Use Not IsEmpty()
.
For example:
Sub DoStuffIfNotEmpty()
If Not IsEmpty(ActiveCell.Value) Then
MsgBox "I'm not empty!"
End If
End Sub
It depends on what you want to test:
- for a string, you can use
If strName = vbNullString
orIF 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)
Extended discussion available here (for Access, but most of it works for Excel as well).
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