Switch Function

See AlsovbfctSwitchSee                 ExamplevbfctSwitchEx>Low


Evaluates a list of expressions and returns a value or an expression associated with the first expression in the list that is True.


Switch( varexpr1, var1 [, varexpr2, var2 . . . [, varexpr7 ,var7 ]] )


The Switch function uses the following parts:

Part                 Description


varexpr             Expression you want to evaluate.

var                   Value or expression that is returned if the corresponding varexpr expression is True.


You can include a maximum of seven varexpr/var pairs.  If the parts aren't properly paired, a run-time error occurs.

The Switch function evaluates the expressions in the varexpr list from left to right and returns the contents of the first var argument whose corresponding varexpr expression evaluates True.  For example, if varexpr1 is True, Switch returns var1. If varexpr1 is False, but varexpr2 is True, Switch returns var2, and so on.

The varexpr part is case-sensitive.  You may want to use UCase on strings before evaluating the expression.

Switch returns a Null1DDW7C0 value if:

         None of the varexpr expressions is True.

         The leftmost varexpr that is True has a corresponding var that is Null.


Switch evaluates all of the expressions, even though it returns only one of them.  For this reason, you should watch for undesirable side effects.  For example, if the evaluation of any varexpr results in a division by zero error, an error occurs.