If statement inserters

If statement inserters in Code VBA are selection statement code blocks integrated with condition menu continuations. which is explained in the next section. Next, I will present the individual If statement inserters.

How to use If statement inserters

Opening the If statement inserters menu

By default they appear when you type if on an empty line in a procedure. You can uncheck 'Entering 'If' triggers 'lf' menu' (in 'Code VBA IntelliSense') Alternatively, you can open the If menu from the Code VBA toolbar.

In the below image you see how you can find the relevant property to use in the condition expression. You then can extend the condition expression using And or Or followed by Shift-Space IntelliSense to select another boolean condition, or select a variable available in the current procedure context. Note the elaborate information made available using the tooltip which helps select a suitable object property, in the example Sheet1.ProtectContents.

The top empty menu option lets you insert the statement block without entering a condition.

If statement inserters with condition menu continuations
If statement inserters with condition menu continuations.

Supplying an Boolean condition expression

As can be seen from the above image continuations menu, Boolean condition expressions can be supplied by:

  • Boolean variables in the code context;
  • Possibly a new variable or new custom function that return a Boolean value;
  • An expression created using the Condition Builder
  • Comparison expressions involving other standard type variables such as here String strFileName;
  • VBA functions and procedures that return a Boolean value;
  • Object properties, first that return a Boolean value, but also returning other standard types.

If statement inserters overview

If continuations menu

The following gives an representative selection of what is availble under the If continuations menu. To illustrate the role and use of variables we assume code insertion starts from the 'if' in the Demo macro below.


Sub Demo()
    Dim strFileName As String: strFileName = ""
    Dim booSignal As Boolean: booSignal = True
    
    if
End Sub

If Then Else End If

The first group of If statement inserters is concerned with variants of the If Then Else syntax. The continuations menu is primarily expecting a Boolean condition expression.

As the tooltip in the image shows, selecting booSignal variable replaces the 'if' by the code block below:


If booSignal Then
    
Else
    
End If

Elself Then

You can expand an If Then expression by adding separate lines with Elself Then.


If intSize <= 10  Then
    strSize = "Small"
ElseIf intSize <= 99  Then
    strSize = "Medium"
Else
    strSize = "Large"
End If

If Then Exit For / Do

When writing loop code it is not uncommon to Exit the loop halfway if a certain condition occurs. For this we included the 'If Then Exit ...' statements.


Do While True
    '...
    If booSignal Then Exit Do
Loop

If Like *

Details on how to work with like can be found in 'Like operator' in MS Learn


If strFileName Like "*.docx" Then
    
Else
    
End If

If Object Is Nothing

It may be necessary to check if indeed a procedure call successfully added a reference to an object. In the below case this would only be so if the active workbook has two sheets.


Dim obj As Object: Set obj = Sheets(2)
If obj Is Nothing Then
    
Else

End If    

If TypeOf Is ...

Snippet inserter dialog

If in the above case the object was found to be set (the Else), you may have reason it is indeed the expected type:


If TypeOf obj Is Worksheet Then
    
Else
    
End If

Select Case Builder ...

Often Select Case is the more appropriate selection construct. For this reason this has been added to the menu here. More details can be found here: Select Case Builder