The SWITCH function evaluates one value (called the expression) against a list of values, and returns the result corresponding to the first matching value. If there is no match, an optional default value may be returned.
Note: This feature is available on Windows or Mac if you have Office 2019, or if you have a Microsoft 365 subscription. If you are a Microsoft 365 subscriber, make sure you have the latest version of Office.
Technical Details
Syntax
SWITCH(expression, value1, result1, [default or value2, result2],…[default or value3, result3])
Argument | Description |
---|---|
expression | Expression is the value (such as a number, date or some text) that will be compared against value1…value126. |
value1…value126 | ValueN is a value that will be compared against expression. |
result1…result126 | ResultN is the value to be returned when the corresponding valueN argument matches expression. ResultN and must be supplied for each corresponding valueN argument. |
default | Default is the value to return in case no matches are found in the valueN expressions. The Default argument is identified by having no corresponding resultN expression (see examples). Default must be the final argument in the function. |
Because functions are limited to 254 arguments, you can use up to 126 pairs of value and result arguments.
Overview
In its simplest form, the SWITCH function says:
-
=SWITCH(Value to switch, Value to match1...[2-126], Value to return if there's a match1...[2-126], Value to return if there's no match)
Where you can evaluate up to 126 matching values and results.
See the following formula:
-
Value to switch? In this case, WEEKDAY(A2) equals 2.
-
What value do you want to match? In this case, it's 1, 2 and 3.
-
If there's a match, what do you want to return as a result? In this case, it would be Sunday for 1, Monday for 2 and Tuesday for 3.
-
Default value to return if there's no match found. In this case, it's the text "No match".
Note: If there are no matching values, and no default argument is supplied, the SWITCH function returns the #N/A! error.
Examples
You can copy the example data in the following table and paste it in cell A1 of a new Excel worksheet to see the SWITCH function in action. If the formulas don't show results, you can select them, then press F2 > Enter. If you need to, you can adjust the column widths to see all the data.
Example
Value | Formula | Result |
---|---|---|
2 | =SWITCH(WEEKDAY(A2),1,"Sunday",2,"Monday",3,"Tuesday","No match") | Because A2=2, and Monday is the result argument corresponding to the value 2, SWITCH returns Monday |
99 | =SWITCH(A3,1,"Sunday",2,"Monday",3,"Tuesday") | Because there's no match and no else argument, SWITCH returns #N/A! |
99 | =SWITCH(A4,1,"Sunday",2,"Monday",3,"Tuesday","No match") | No match |
2 | =SWITCH(A5,1,"Sunday",7,"Saturday","weekday") | weekday |
3 | =SWITCH(A6,1,"Sunday",2,"Monday",3,"Tuesday","No match") | Tuesday |
Need more help?
You can always ask an expert in the Excel Tech Community, get support in the Answers community, or suggest a new feature or improvement on Excel User Voice.
See Also
Find and correct errors in formulas
Excel keyboard shortcuts and function keys
No comments:
Post a Comment