Select...Case Statement

Executes one block of statements out of many, depending on a test expression.

Syntax

Select [ Case ] testExpression [ Tol tolerance ]
  Case expressionList
    [ statements ]
  ...
  [ Case Else
    [ elseStatements ] ]
End Select

Parts

testExpression
Required expression of an elementary data type.
tolerance
Optional floating-point constant specifies the tolerance for rounding errors in comparisons. See Tol Clause for more information.
expressionList

Required in a Case clause. A comma-separated list of expressions to match against testExpression. The expression can take one of the following forms:

Range form
expression1 To expression2
Comparison form
Is comparisonOp expression
Simple form
expression

The range form permits matching a range of values. The value of expression1 must be less than or equal to expression2.

The comparison form permits open-ended comparisons using any comparison operator (<, <=, >, >=, =, or <>).

The simple form is a specialized comparison form which tests for equality with testExpression.

statements
Optional. One or more statements following Case that execute if testExpression matches any clause in expressionList.
elseStatements
Optional. One or more statements following Case Else that execute only if no clause in any expressionList in any Case matches testExpression.
End Select
Terminates the statement block.

Remarks

If testExpression matches a Case expressionList clause, then control moves to the block of statements that follow it and continues until the next Case, Case Else, or End Select. Control then passes to the statement following End Select. If more than one match is possible, only the first matching Case gets executed.

The Case Else clause is used to introduce the elseStatements, which get executed only when there are no matches between testExpression and any of the Case expressionList clauses. Although not a requirement, it is considered a good practice to include one to handle any unexpected values of testExpression. If there are no matching Case clauses and no Case Else, then control passes to the statement following End Select.

Examples

See also