MS Excel: SWITCH Function (VBA)
Learn how to use the Excel SWITCH function with syntax and examples.
The Microsoft Excel 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 Microsoft Excel SWITCH function is:
Switch ( expression1, value1, expression2, value2, ... expression_n, value_n )
Parameters or Arguments
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.
The SWITCH function can be used in the following versions of Microsoft Excel:
- Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000
Type of Excel Function
The SWITCH function can be used in Microsoft Excel as the following type of function:
- VBA function (VBA)
Example (as VBA Function)
The SWITCH function can only be used in VBA code in Microsoft Excel.
Let's look at some Excel SWITCH function examples and explore how you would use the SWITCH function in Excel VBA code:
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".
Dim LValue As String LValue = Switch (SupplierID=1, "IBM", SupplierID=2, "HP", SupplierID=3, "NVIDIA")