Excel: Switch Function (VBA only)
In Excel, the Switch function evaluates a list of expressions and returns the corresponding value for the first expression in the list that is TRUE.
The syntax for the Switch function is:
Switch ( expression1, value1, expression2, value2, ... expression_n, value_n )
expression1, expression2, expression_n is a list of expressions that are evaluated. The Switch function is looking for the first expression that evaluates to TRUE.
value1, value2, ... value_n is a list of values. The Switch function will return the value associated with the first expression that evaluates to TRUE.
Applies To:
- Excel 2007, Excel 2003, Excel XP, Excel 2000
For example:
Switch (SupplierID=1, "IBM", SupplierID=2, "HP", SupplierID=3, "NVIDIA")
In this example, if SupplierID is 1, then the Switch function will return "IBM".
If SupplierID is 2, then the Switch function will return "HP".
If SupplierID is 3, then the Switch function will return "NVIDIA".
VBA Code
The Switch function can only be used in VBA code. For example:
Dim LValue As String
LValue = Switch (SupplierID=1, "IBM", SupplierID=2, "HP", SupplierID=3, "NVIDIA")
