See Also Example
Executes one of several statement blocks depending on the value of an expression.
Syntax
Select Case testexpression
[Case expressionlist1
[statementblock-1] ]
[Case expressionlist2
[statementblock-2] ]
[Case Else
[statementblock-n] ]
End Select
Remarks
The Select Case syntax has these parts:
Part Description
Select Case Begins the Select Case decision control structure. Must appear before any other part of the Select Case structure.
testexpression Any numeric or string expression . If testexpression matches the expressionlist associated with a Case clause, the statementblock following that Case clause is executed up to the next Case clause, or for the final one, up to the End Select. Control then passes to the statement following End Select. If testexpression matches more than one Case clause, only the statements following the first match are executed.
Case Sets apart a group of Visual Basic statements to be executed if an expression in expressionlist matches testexpression.
expressionlist The expressionlist consists of a comma-delimited list of one or more of the following forms.
expression
expression To expression
Is compare-operator expression
statementblock Elements statementblock-1 to statementblock-n consist of any number of Visual Basic statements on one or more lines.
Case Else Keyword indicating the statementblock to be executed if no match is found between the testexpression and an expressionlist in any of the other Case selections. When there is no Case Else statement and no expression listed in the Case clauses matches testexpression, program execution continues at the statement following End Select.
End Select Ends the Select Case. Must appear after all other statements in the Select Case control structure.
The argument expressionlist has these parts:
Part Description
expression Any numeric or string expression. The type of the expression must be compatible with the type of testexpression. (The type of the expression will be coerced to the same type as testexpression. For example, if testexpression is an integer, expressionlist can contain a double-precision expression, but it will be coerced to an integer.)
To Keyword used to specify a range of values. If you use the To keyword to indicate a range of values, the smaller value must precede To.
Is Keyword used with comparison operators to specify a range of values. If not provided, it is automatically inserted.
compare-operator Any valid comparison operator , except Is and Like.
Note Do not confuse the Is comparison operator with the Is keyword used in the Select Case statement. They are not the same.
Although not required, it is a good idea to have a Case Else statement in your Select Case block to handle unforeseen testexpression values.
You can use multiple expressions or ranges in each Case clause. For example, the following line is valid:
Case 1 To 4, 7 To 9, 11, 13, Is > MaxNumber
You also can specify ranges and multiple expressions for character strings. In the following example, Case matches strings that are exactly equal to everything, strings that fall between nuts and soup in alphabetical order, and the current value of TestItem$:
Case "everything", "nuts" To "soup", TestItem$
Select Case statements can be nested. Each Select Case statement must have a matching End Select statement.
If...Then...Else Statement
On...GoSub, On...GoTo Statements
Option Compare Statement
The example uses Select Case to decide what action to take based on user input. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
Dim Msg, UserInput ' Declare variables.
Msg = "Please enter a letter or number from 0 through 9."
UserInput = InputBox(Msg) ' Get user input.
If Not IsNumeric(UserInput) Then ' Is it letter or number?
If Len(UserInput) <> 0 Then
Select Case Asc(UserInput) ' If it's a letter.
Case 65 To 90 ' Must be uppercase.
Msg = "You entered the uppercase letter '"
Msg = Msg & Chr(Asc(UserInput)) & "'."
Case 97 To 122 ' Must be lowercase.
Msg = "You entered the lower-case letter '"
Msg = Msg & Chr(Asc(UserInput)) & "'."
Case Else ' Must be something else.
Msg = "You did not enter a letter or a number."
End Select
End If
Else
Select Case CDbl(UserInput) ' If it's a number.
Case 1, 3, 5, 7, 9 ' It's odd.
Msg = UserInput & " is an odd number."
Case 0, 2, 4, 6, 8 ' It's even.
Msg = UserInput & " is an even number."
Case Else ' Out of range.
Msg = "You entered a number outside "
Msg = Msg & "the requested range."
End Select
End If
MsgBox Msg ' Display message.
End Sub