Specifications
Section 9. Program Control Instructions
9-15
the Case clauses matches testexpression, program execution
continues at the statement following EndSelect.
EndSelect 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 expression.
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.
Although not required, it is a good idea to have a CaseElse statement in your
SelectCase 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
SelectCase statements can be nested. Each SelectCase statement must have a
matching EndSelect statement.
SelectCase Example
The example uses SelectCase to decide what action to take based on user input.
Dim X, Y 'Declare variables.
If Not X = Y Then 'Are they equal
If X > Y Then
SelectCase X 'What is X.
Case 0 To 9 'Must be less than 10.
. . . . 'Run some code.
. . . . 'Run some code.
Case 10 To 99 'Must be less than 100.
. . . . 'Run some code.
. . . . 'Run some code.
CaseElse 'Must be something else.
. . . . 'Run some code.
EndSelect
EndIf
Else
SelectCase Y 'What is Y.
Case 1, 3, 5, 7, 9 'It's odd.
. . . . 'Run some code.
Case 0, 2, 4, 6, 8 'It's even.
. . . . 'Run some code.
CaseElse 'Out of range.
. . . . 'Run some code.
. . . . 'Run some code.
EndSelect
EndIf
. . . . 'Run some code.
. . . . 'Run some code.