The SWITCH function
SWITCH is a neat way to translate codes. Give it one cell, then pairs of "if it's this, show that", and an optional default at the end.
Unlike IFS it only checks for exact matches, which keeps the formula short: you write the cell once instead of in every test.
=SWITCH(expression, value1, result1, [value2, result2], ..., [default])
Arguments
- expression
- The value to check, usually a cell.
- value1, result1
- If the expression equals value1, SWITCH returns result1.
- value2, result2, ...optional
- More value and result pairs.
- defaultoptional
- A final single value to return when nothing matches. Without it you get #N/A.
Try it
=SWITCH(B2,"STD","3-5 days","EXP","1-2 days","NXT","Next day","Check") in C2. B2 is EXP, which matches the second pair, so SWITCH returns 1-2 days.
C2
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Order | Method | Delivery | |
| 2 | #2051 | EXP | 1-2 days | |
| 3 | #2052 | STD | ||
| 4 | #2053 | CLC | ||
| 5 | #2054 | NXT | ||
| 6 | ||||
| 7 |
Edit the formula in C2, or change the data, and press Enter.
More examples
| Formula | Result | What it does |
|---|---|---|
| =SWITCH(B3,"STD","3-5 days","EXP","1-2 days","NXT","Next day","Check") | 3-5 days | STD matches the first pair. |
| =SWITCH(B4,"STD","3-5 days","EXP","1-2 days","NXT","Next day","Check") | Check | CLC isn't listed, so the default at the end is used. |
| =SWITCH(B4,"STD","3-5 days","EXP","1-2 days") | #N/A | No match and no default gives #N/A. |
| =SWITCH(3,1,"Mon",2,"Tue",3,"Wed") | Wed | It works with numbers too. |
Tips
- SWITCH is in Excel 2019, Excel 2021 and Microsoft 365, but not Excel 2016 or earlier.
- Always add a default, even if it's just
"Unknown", so new codes don't show #N/A. - For ranges of values (over 50, over 70), use IFS instead: SWITCH only does exact matches.
Common mistakes
- `SWITCH(B2,">50",…)` never matches.SWITCH compares for equality, so ">50" is just text. Use IFS for comparisons.
- The default appears when a code looks right.Check for trailing spaces in the data, or a number stored as text: 3 and "3" aren't equal.
Practise SWITCH with instant feedback
Type real formulas into a working spreadsheet and get told straight away whether they work.