Select Case statement
Execute several sets of statements based on the expression value.
Select Case testexpression
[Case expressionlist-n
[statements-n]] . . .
[Case Else expressionlist-n
[elsestatements-n]]
End Select
Parameters
Testexpression
Any number or string expression.
Expressionlist-n
For exampleCaseThis parameter is required when it appears. List of the boundaries of one or more expressions.
Statements-n
One or more statements executed when testexpression matches any part of expressionlist-n.
Elsestatements-n
WhenTestexpressionAndCaseIf any part of the clause does not match, one or more statements are executed.
Description
IfTestexpressionWith anyCase ExpressionlistIf the expression matches, run the following command:CaseClause and nextCaseStatement Between clauses. For the last clause, the clause is executedEnd SelectAnd then the control is forwardedEnd Select. For exampleTestexpressionWith multipleCaseClauseExpressionlistIf the expression matches, only the statement after the first match is executed.
Case Else indicates that elsestatements is executed if no match is found between testexpression and expressionlist of any other Case options. Although not necessary, it is best to place the Case Else statement in the Select Case block to handle unforeseen testexpression values. If no Case expressionlist matches testexpression and no Case Else statement expressionlist, the statement after End Select is executed.
Select CaseThe statement can be nested, with each layer nestedSelect CaseThe statement must matchEnd SelectStatement.
The following example illustrates how to useSelect CaseStatement:
Dim Color, MyVarSub ChangeBackground (Color)
MyVar = lcase (Color)
Select Case MyVar
Case "red" document.bgColor = "red"
Case "green" document.bgColor = "green"
Case "blue" document.bgColor = "blue"
Case Else MsgBox "
Select another color"
End Select
End Sub