Lesson 84 of 92 · Free Excel Course
Excel · Excel function reference
Excel SWITCH Function: Syntax, Examples and Common Errors
How the Excel SWITCH function matches one value against a list, the SWITCH(TRUE) trick for ranges, SWITCH vs IFS vs nested IF, and how to fix #N/A and #NAME?.
The SWITCH function takes one value, compares it with a list of possible values, and returns the result paired with the first one that matches. If nothing matches it returns an optional default, or the #N/A error if you didn't give one. It's a tidy replacement for a nested IF that keeps asking "is it this? is it that?" about the same cell, such as turning the code S into "Shipped". Microsoft lists SWITCH for Excel for Microsoft 365 (Windows and Mac), Excel for the web, Excel 2024, Excel 2021 and Excel 2019. Excel 2016 doesn't have it.
Syntax
=SWITCH(expression, value1, result1, [value2, result2], ..., [default])
- expression (required): the value to test, written once. It can be a cell (
B2), a number, text, a date, or a formula such asWEEKDAY(A2). - value1 (required): the first value to compare with the expression. A match must be exact; text is not case-sensitive, but the text "1" doesn't match the number 1.
- result1 (required): what to return when the expression equals value1. It can be text, a number, a cell or a whole calculation.
- value2, result2 … (optional): more pairs, up to 126 in total. Excel functions can take at most 254 arguments, which sets the limit.
- default (optional): what to return when nothing matches. It's the one argument with no paired result, and it must come last. Leave it out and a non-match gives
#N/A.
SWITCH checks the pairs from left to right and stops at the first match.
Examples
Microsoft's examples. Column A holds 2, 99, 99, 2 and 3. Each row uses a slightly different SWITCH.
| A | B | |
|---|---|---|
| 1 | Value | Result |
| 2 | 2 | Monday |
| 3 | 99 | #N/A |
| 4 | 99 | No match |
| 5 | 2 | weekday |
| 6 | 3 | Tuesday |
| Cell | Formula | Result |
|---|---|---|
| B2 | =SWITCH(WEEKDAY(A2),1,"Sunday",2,"Monday",3,"Tuesday","No match") | Monday: WEEKDAY gives 2 |
| B3 | =SWITCH(A3,1,"Sunday",2,"Monday",3,"Tuesday") | #N/A: no match, no default |
| B4 | =SWITCH(A4,1,"Sunday",2,"Monday",3,"Tuesday","No match") | No match: the default |
| B5 | =SWITCH(A5,1,"Sunday",7,"Saturday","weekday") | weekday: 2 isn't 1 or 7 |
| B6 | =SWITCH(A6,1,"Sunday",2,"Monday",3,"Tuesday","No match") | Tuesday |
Codes to labels, with a default. An order export uses one-letter codes. SWITCH turns them into words, and the default catches any code you didn't plan for.
| A | B | C | |
|---|---|---|---|
| 1 | Order | Code | Status |
| 2 | 1041 | P | Processing |
| 3 | 1042 | S | Shipped |
| 4 | 1043 | D | Delivered |
| 5 | 1044 | X | Unknown code |
A different calculation for each case. Results don't have to be text. Here Gold members get 20% off, Silver 10% off, and everyone else (the default) pays the full price.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Member | Tier | Price | You pay |
| 2 | Ana | Gold | $80.00 | $64.00 |
| 3 | Ben | Silver | $45.00 | $40.50 |
| 4 | Cal | Basic | $30.00 | $30.00 |
Ranges with SWITCH(TRUE). SWITCH can't take >= in a value, but you can turn it around: make the expression TRUE and write a test in place of each value. Each test works out to TRUE or FALSE, and SWITCH returns the result next to the first one that equals TRUE. Here the commission rate is 8% from $10,000 of sales, 5% from $5,000, otherwise 2%.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Rep | Sales | Rate | Commission |
| 2 | Kim | $12,400 | 8% | $992.00 |
| 3 | Lou | $7,250 | 5% | $362.50 |
| 4 | Mo | $3,900 | 2% | $78.00 |
| 5 | Nia | $5,000 | 5% | $250.00 |
It works, but the same job can be written several ways. All four below give identical rates for the data above:
| Method | Formula for row 2 | Best when |
|---|---|---|
| SWITCH(TRUE) | =SWITCH(TRUE,B2>=10000,0.08,B2>=5000,0.05,0.02) | You already use SWITCH elsewhere |
| IFS | =IFS(B2>=10000,0.08,B2>=5000,0.05,TRUE,0.02) | Ranges: the clearest choice (Excel 2019+) |
| Nested IF | =IF(B2>=10000,0.08,IF(B2>=5000,0.05,0.02)) | The file must open in Excel 2016 or older |
| XLOOKUP table | =XLOOKUP(B2,$I$2:$I$4,$J$2:$J$4,,-1) | Rates change often: edit the table, not the formula |
The XLOOKUP version reads a small table (0, 5,000 and 10,000 in I2:I4, with 2%, 5% and 8% next to them); -1 means "exact match or the next smaller value". For exact codes, plain SWITCH is the shortest of all.
Common errors
#N/A: nothing matched and there's no default. Add one as the last argument. If a match should exist, look for a trailing space ("S " isn't "S") or a number stored as text, since "1" and 1 never match.#NAME?: the name is misspelled, or the file is open in Excel 2016 or earlier, which doesn't have SWITCH. Use a nested IF or a lookup table if the file must work there.- Every row returns the default: you typed a comparison as a value, such as
=SWITCH(B2,">=100","Big","Small"). SWITCH compares B2 with the text ">=100", which never matches. Use SWITCH(TRUE) or IFS. - Wrong band in SWITCH(TRUE): the first TRUE wins, so order matters.
=SWITCH(TRUE,B2>=5000,0.05,B2>=10000,0.08,0.02)gives Kim 5%, because $12,400 passes the 5,000 test first. Put the highest threshold first. - A strange result for anything not in the list: you forgot the last result. In
=SWITCH(A2,1,"One",2), the 2 has no partner, so Excel treats it as the default and returns 2 for every value except 1. Count your arguments: pairs plus at most one extra.
Related functions
- IF, IFS and nested IF: the lesson that covers IFS and nested IF, the alternatives to SWITCH for ranges.
- XLOOKUP and INDEX/MATCH: a lookup table is easier to maintain than a long SWITCH.
- IFNA function: replaces the
#N/Aa lookup returns when nothing matches. - NOT function: reverses a TRUE/FALSE test inside SWITCH(TRUE) or IF.
Try it yourself
Download the practice workbook. Fill in the yellow cells for Microsoft's day-name examples, the status codes, the member prices and the SWITCH(TRUE) commission table, then compare with the Answers sheet, which also shows the IFS, nested IF and XLOOKUP versions side by side.
Frequently asked questions
What is the difference between SWITCH and IFS?
SWITCH checks one value against a list of exact matches, such as a code that can be P, S or D, and you write that value only once. IFS takes a separate test for each answer, so it can use greater than and less than. Use SWITCH for codes and names, IFS for ranges of numbers. IF and IFS covers IFS in detail.
Can the SWITCH function use greater than or less than?
Not directly: SWITCH only looks for exact matches. The workaround is to use TRUE as the expression and write a test in place of each value, such as =SWITCH(TRUE,B2>=10000,0.08,B2>=5000,0.05,0.02). SWITCH returns the result after the first test that is TRUE, so put the tests in order from the highest threshold down.
Why does my SWITCH formula return #N/A?
Nothing in the list matched and you didn't give a default. Add a final argument with no paired result, such as "Unknown", and SWITCH returns it instead. If you think there should be a match, check for extra spaces or a number stored as text.
Why does SWITCH give a #NAME? error?
Your version of Excel doesn't have SWITCH. It needs Excel 2019 or later, Microsoft 365 or Excel for the web; Excel 2016 doesn't include it. Use a nested IF or a VLOOKUP table there instead.
How many values can SWITCH check?
Up to 126 value and result pairs. Excel functions can take at most 254 arguments, which is where the limit comes from. Long before that, a small lookup table with XLOOKUP or VLOOKUP is easier to keep up to date.