VBA If Statements: Using Control Structures to Make Decisions

A control structure is a combination of keywords in code used to make a decision that alters the flow of code the computer executes.

If…Then

The first control structure you should know about is If…Then. The syntax for this control structure is given as:

 If condition = True Then
   ...the code that executes if the condition is satisfied 
End If

…where condition is some test you want to apply to the conditional structure. If the condition is true, the code within the If and End If statements is executed. If the condition is not true, the code within these statements is skipped over and does not get executed.

If…Then…Else

What if you want to do one thing if the condition is true but another thing if the condition is false? You could use two statements:

If condition = True Then 
   Msgbox "The condition is true!" 
End If 
If condition = False Then
   Msgbox "The condition is false!"
End If

…but wouldn’t it be nice if you didn’t have to repeat the test. Fortunately, you have another control structure available to you in VBScript that makes this process easier. The control structure, called If…Then…Else, is represented as

If condition = True Then 
   ...this is the code that executes if the condition is satisfied 
Else 
   ...this is the code that executes if the condition is not satisfied 
End If

ElseIf

What if you had a few other cases you wanted to test? You’re in luck: You can do as many tests as you want by simply placing ElseIf statements between the first If statement and the End If statement. The syntax of such a structure looks like this:

If condition1 = True Then 
   ...the code that executes for condition1 
ElseIf condition2 = True Then 
   ...the code that executes for condition2 
ElseIf condition3 = True Then 
   ...the code that executes for condition3
Else
   ...the code that executes if none of the above are true
End If

Select Case

What if you have to perform a large number of tests on the same expression? The Select statement often makes your code easier to read and interpret than would a long list of Else and Else If statements. The Select Case structure is defined as follows:

Select Case test_expression 
   Case expression-1
      ...this is the code that executes if expression-1 matches test_expression 
   Case expression-2 
      ...this is the code that executes if expression-2 matches test_expression 
   Case expression-3 
      ...this is the code that executes if expression-3 matches test_expression 
   Case Else 
      ...this is the code that executes if expression-n matches test_expression 
End Select

Consider an example in which you want to print a message to the user based on what state the user lives in:

Select Case State 
   Case "Michigan" 
      Message = "Michigan is a wonderful state to visit if you enjoy fresh-water lakes." 
   Case "Virginia" 
      Message = "Visit the Commonwealth for a wide variety of historical landmarks and beautiful mountain-scapes."
   Case "Arizona" 
      Message = "Arizona is a wonderful getaway for those who love heat with low humidity"
   Case "Colorado" 
      Message = "Colorado is almost unsurpassed for its majestic mountains and rivers." 
   Case Else
      Message = "No specific information is available about this state." 
End Select

Contact Us