VBA Check if variable is empty

calendar_today Asked Jul 25, 2010
thumb_up 44 upvotes
history Updated April 16, 2026

Question posted 2010 · +21 upvotes

I have an object and within it I wanna check if some properties is set to false, like:

If (not objresult.EOF) Then
  'Some code 
End if

But somehow, sometimes objresult.EOF is Empty, and how can I check it?

  • IsEmpty function is for excel cells only
  • objresult.EOF Is Nothing – return Empty
  • objresult.EOF <> null – return Empty as well!

Accepted answer +44 upvotes

How you test depends on the Property’s DataType:

| Type                                 | Test                            | Test2
| Numeric (Long, Integer, Double etc.) | If obj.Property = 0 Then        | 
| Boolen (True/False)                  | If Not obj.Property Then        | If obj.Property = False Then
| Object                               | If obj.Property Is Nothing Then |
| String                               | If obj.Property = "" Then       | If LenB(obj.Property) = 0 Then
| Variant                              | If obj.Property = Empty Then    |

You can tell the DataType by pressing F2 to launch the Object Browser and looking up the Object. Another way would be to just use the TypeName function:MsgBox TypeName(obj.Property)

Top vba Q&A (6)

+44 upvotes ranks this answer #6 out of 81 vba solutions on this site — top 7%.
vba