The Excel Switch function compares a number of supplied values to a supplied test expression and returns a result corresponding to the first value that matches the test expression. A default value can be supplied, to be returned if none of the supplied values match the test expression.
The syntax of the Switch function is:
where the arguments are as follows:
|expression||-||The expression or value that is to be compared to each of the supplied values.|
|-||The values that are to be compared to the supplied expression.|
|-||The results that are to be returned if the corresponding value matches the supplied expression.|
|[default]||-||An optional default value that is to returned if none of the supplied values match the supplied expression.|
The following Switch function examples return a person's name corresponding to the subject number that is stored in column A of the spreadsheet. If the number in column A does not match one of the subject numbers 1-3, the function returns the text string "Invalid Subject Number".
For further details and examples of the Excel Switch function, see the Microsoft Office website.
If you get an error from the Excel Switch function, this is likely to be one of the following:
|#N/A||-||Occurs if none of the supplied values match the supplied expression and no default argument has been supplied to the Switch function.|
|#NAME?||-||Occurs if you are using an older version of Excel (pre-2016), that does not support the Switch function.|