|
Use the Select Case statement as an alternative to using ElseIf in If...Then...Else statements when comparing one expression to several different values. While If...Then...Else statements can evaluate a different expression for each ElseIf statement, the Select Case statement evaluates an expression only once, at the top of the control structure.
In the following example, the Select Case statement evaluates the 'my_var' variable that is passed. Note that each Case statement can contain more than one value, a range of values, or a combination of values and comparison operators. The optional Case Else statement runs if the Select Case statement doesn't match a value in any of the Case statements.
Dim my_var
InputBox my_var, "Enter a value"
Select Case my_var
Case 1
MsgBox "Match 1"
Case 2
MsgBox "Match 2"
Case Else
MsgBox "No Match"
End Select
Note
The 'Contains' operator does not work in Case statements.