Since the answer is one of the top ranked in Google just looking for something like vba if condition not lazy
I would like to provide a simpler example, the problem and solutions of both conditions: AND
and the more interesting OR
...
Dim cond1 As Boolean 'some 1st condition that may be True or False
Dim obj As Collection 'just some sample object that may or may not be instantiated
(²: I find it better to explain other devs, why you did not choose OR
if they don't know the background)
the AND
case
cond1 = False
If cond1 Then Set obj = New Collection
problem:
If cond1 And obj.Count > 0 Then Debug.Print "Count > 0!" 'throws error if < cond1 = False >
'because condition 2 is always evaluated
solution:
If cond1 Then If obj.Count > 0 Then Debug.Print "Count > 0!" 'AND would not short-cicuit!² https://stackoverflow.com/a/57521572/1915920
Depending on taste, complexity and readability it may make sense to write it this way:
If cond1 Then
If obj.Count > 0 Then 'AND would not short-cicuit!² https://stackoverflow.com/a/57521572/1915920
Debug.Print "Count > 0!"
End If
End If
the OR
case
cond1 = True
If Not cond1 Then Set obj = New Collection 'obj stays < Nothing > otherwise
problem:
If cond1 Or obj.Count = 0 Then Debug.Print "no objects!" 'throws error if < cond1 = True >
'because condition 2 is always evaluated
solution 1:
in-place, non-redundant one-liner without GoTo
using Select
:
Select Case True: Case cond1, obj.Count = 0: Debug.Print "no objects!": End Select 'OR would not short-cicuit!² https://stackoverflow.com/a/57521572/1915920
in case it should/must be on multiple lines and with some else:
Select Case True
Case cond1, obj.Count = 0 'OR would not short-cicuit!² https://stackoverflow.com/a/57521572/1915920
Debug.Print "no objects!"
Case Else
Debug.Print "object count: " & obj.Count
End Select
solution 2:
in-place, non-redundant code with minimal GoTo
usage, but more lengthy If
-multi-line code:
If cond1 Then
noObjs:
Debug.Print "no objects!"
ElseIf obj.Count = 0 Then 'OR would not short-cicuit!² https://stackoverflow.com/a/57521572/1915920
GoTo noObjs
End If
solution 3:
in-place, conditions (may fit) on one line similar to OR
-concatenation with quite some GoTo
usage:
If cond1 Then GoTo noObjs ElseIf obj.Count = 0 Then GoTo noObjs 'OR would not short-cicuit!² https://stackoverflow.com/a/57521572/1915920
GoTo skipOnAllFalse
noObjs:
Debug.Print "no objects!"
skipOnAllFalse: 'use more specific label/scenario name if possible
solution 4:
out-of-place code (Sub
), avoiding GoTo
, conditions (may fit) on one line, but module/class code may be more unreadable/spread/cluttered:
Private Sub noObjs(): Debug.Print "no objects!"
If cond1 Then noObjs ElseIf obj.Count = 0 Then noObjs 'OR would not short-cicuit!² https://stackoverflow.com/a/57521572/1915920
solution 5:
using one condition variable:
Dim any As Boolean: any = cond1
If Not any Then any = obj.Count = 0 'OR would not short-cicuit!² https://stackoverflow.com/a/57521572/1915920
If any Then Debug.Print "no objects!"
solution 6:
using multiple condition variables:
Dim c1 As Boolean: Dim c2 As Boolean
c1 = cond1
If Not c1 Then c2 = obj.Count = 0 'OR would not short-cicuit!² https://stackoverflow.com/a/57521572/1915920
If c1 Or c2 Then Debug.Print "no objects!" 'safe to use Or now
And
operator doesn't short circuit because it is a bitwise operator and not a logical one. See: stackoverflow.com/questions/8042744/… – jtolleOr
was "all 1s" (&HFFFFFFFF
, or-1&
), and likewise forAnd
if the left argument was 0. – Hugh Allen