AndAlso/OrElse in VBA

calendar_today Asked Jul 14, 2010
thumb_up 36 upvotes
history Updated April 16, 2026

Question posted 2010 · +26 upvotes

I’m trying to get a lazy evaluation with ‘And’ in my Excel macro by doing the following:

If Not myObject Is Nothing *And* myObject.test() Then
    'do something'
Else
    'do something else'
End If

I know lazy evaluation exists in VB.NET as AndAlso and OrElse but cannot find anything similar in VBA. If lazy evaluation does not exist in VBA, what’s the best way to structure the code so that it will evaluate the way I expect?

Accepted answer +36 upvotes

The only short circuiting (of a sort) is within Case expression evaluation, so the following ungainly statement does what I think your asking;

Select Case True
    Case (myObject Is Nothing), Not myObject.test()
        MsgBox "no instance or test == false"
    Case Else
        MsgBox "got instance & test == true"
    End Select
End Sub

Top vba Q&A (6)

+36 upvotes ranks this answer #9 out of 81 vba solutions on this site — top 11%.
vba