Switch (function)


Switch(condition1,expression1 [,condition2,expression2 ... [,condition7,expression7]])


Returns the expression corresponding to the first True condition.


The Switch function evaluates each condition and expression, returning the expression that corresponds to the first condition (starting from the left) that evaluates to True. Up to seven condition/expression pairs can be specified.

A runtime error is generated it there is an odd number of parameters (that is, there is a condition without a corresponding expression).

The Switch function returns Null if no condition evaluates to True.


The following code fragment displays the current operating platform. If the platform is unknown, then the word "Unknown" is displayed.

Sub Main()
  Dim a As Variant
  a = Switch(Basic.OS = 0,"Windows XP",Basic.OS = 2,"Win32",Basic.OS = 11,"OS/2")
  MsgBox "The current platform is: " & IIf(IsNull(a),"Unknown",a)
End Sub

See Also

Choose (function); IIf (function); If...Then...Else (statement); Select...Case (statement).

More information