Select Case Statement |
Select Case testexpression [Case expressionlist-n [ statements-n ]] ... [Case Else [ elsestatements ]] End Select
Any numeric or string expression whose value determines which block of code is executed
Comma-delimited list of expressions to compare values with testexpression
Program statements to execute if a match is found between any section of expressionlist and testexpression
Program statements to execute if a match between testexpression and any expressionlist cannot be found
expressionlist can use any (or a combination of any) of the following:
expressionlist syntax | Examples |
---|---|
expression | iVar - iAnotherVar iVar |
expression To expression | 5 To 10 8 To 11, 13 to 15 "A" To "D" |
Is comparisonoperator expression | Is = 10 |
Allows for conditional execution of a block of code, typically out of three or more code blocks, based on some condition. Use the Select Case statement as an alternative to complex nested If...Then...Else statements.
Any number of Case clauses can be included in the Select Case statement.
If a match between testexpression and any part of expressionlist is found, the program statements following the matched expressionlist will be executed. When program execution encounters the next Case clause or the End Select clause, execution will continue with the statement immediately following the End Select clause.
If multiple Case statements are True , only the statements belonging to the first true Case statement are executed.
If used, the Case Else clause must be the last Case clause. Program execution will only encounter the Case Else clause and thereby execute the elsestatements if all other expressionlist comparisons have failed.
Use the To keyword to specify a range of values. The lower value must precede the To clause, and the higher value follow it. Failure to do this does not generate a syntax error. Instead, it causes the comparison of the expression with testexpression to always fail, so that program execution falls through to the Case Else code block, if one is present.
The Is keyword is used to precede any comparison operators. For example:
Case Is >= 100
Select Case statements can also be nested.
The following example uses Select Case to act based on the response to a MsgBox function:
Select Case MsgBox("Backup file before changing.", vbYesNoCancel) Case vbYes ' do something Case vbNo ' do something Case vbCancel ' do something End Select
The Select Case statement is the VB equivalent of the Switch construct found in C and C++.
The Case Else clause is optional. However, as with If...Then...Else statements, it is often good practice to provide a Case Else to catch the exceptional instance when perhaps unexpectedly a match cannot be found in any of the expressionlists you have provided.
The To clause can be used to specify ranges of character strings. However, it is often difficult to predict the thousands of possible combinations of valid characters between two words that will be successfully matched by Select Case .
The Is keyword used in the Select Case statement is not the same as the Is comparison operator.
Multiple conditions in a single Case statement are evaluated separately, not together; that is, they are connected with a logical OR , not a logical AND . For example, the statement:
Case Is > 20, Is < 40
will evaluate to True whenever the value of testexpression is greater than 20. In this case, the second comparison is never evaluated; it is evaluated only when testexpression is under 20. This suggests that if you use anything other than the most straightforward conditions, you should test them thoroughly.
If...Then...Else Statement