Select...Case Statement

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

Syntax

Select [ Case ] test_expression [ Tol tolerance ]
  Case expression_list
    [ statements ]
  ...
  [ Case Else
    [ else_statements ] ]
End [ Select ]

Parts

test_expression
Necessary 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.
expression_list

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

Range form

expression_1 To expression_2

Comparison form

Is comparison_op expression

Simple form

expression

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

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 test_expression.

statements
Optional. One or more statements following Case that execute if test_expression matches any clause in expression_list.
else_statements
Optional. One or more statements following Case Else that execute only if no clause in any expression_list in any Case matches test_expression.
End
Completes the statement. Alternatively, you can use End Select.

Instructions

If test_expression matches a Case expression_list 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 else_statements, which get executed only when there are no matches between test_expression and any of the Case expression_list clauses. Although not a requirement, it is considered a good practice to include one to handle any unexpected values of test_expression. If there are no matching Case clauses and no Case Else, then control passes to the statement following End Select.

Examples

See also