The SWITCH Function in Excel (Turn Codes Into Labels)
By ExcelReady TeamUpdated 7 min read
The short answer
The Excel SWITCH function compares one value with a list of options and returns the result paired with the first exact match: =SWITCH(D2,"P","Pending","S","Shipped","Other"). The last single value is the default, shown when nothing matches. Without a default, SWITCH returns #N/A. SWITCH needs Excel 2019 or later.
Key takeaways
- SWITCH checks one value against exact options; each option is followed by its result.
- An extra value at the very end is the default, shown when no option matches.
- Without a default, a value with no match returns #N/A.
- SWITCH only does exact matches; for ranges like >=90, use IFS or nested IF.
- SWITCH needs Excel 2019, Excel 2021 or Microsoft 365; older versions show #NAME?.
On this page+
The Excel SWITCH function turns short codes into readable labels. You give it one value, such as a status code, and a list of possible matches with their results. It returns the result for the first exact match, or a default if nothing matches. This guide covers the syntax, the default value, and when IFS or a lookup table is the better tool.
How does the Excel SWITCH function work?#
SWITCH compares one value, the expression, with each option in turn. When an option matches exactly, it returns the result paired with it:
=SWITCH(expression, value1, result1, [value2, result2], ..., [default])| Part | What it means | Example |
|---|---|---|
expression | The value to check, usually a cell | D2 |
value1, result1 | An option and the result to show when it matches | "P","Pending" |
default | Optional. One extra value at the end, shown when nothing matches | "Check code" |
You can list up to 126 pairs. SWITCH works in Excel 2019, Excel 2021, Excel 2024, Microsoft 365 and Excel for the web.
How to turn codes into labels with SWITCH#
An order export often stores status as one letter. SWITCH translates each code:
=SWITCH(D2,"P","Pending","S","Shipped","D","Delivered")| Order | Code | Status |
|---|---|---|
| 2041 | S | Shipped |
| 2042 | P | Pending |
| 2043 | D | Delivered |
| 2047 | R | #N/A |
Text codes and labels both need double quotes. Numbers don't: =SWITCH(B2,1,"High",2,"Medium",3,"Low") works on
numeric codes.
Look at the last row. R isn't in the list, so SWITCH has nothing to return and shows #N/A.
To build the formula yourself:
- Click the first empty cell of the Status column and type
=SWITCH(. - Click the code cell, D2, and type a comma.
- Type each code and its label as a pair, with commas between them:
"P","Pending","S","Shipped",. - Finish the last pair, close the bracket and press Enter.
- Fill down with Ctrl+D (Windows) or ⌘D (Mac). D2 changes to D3, D4 and so on.
Check the pairs before you fill down. If a code and its label are the wrong way round, SWITCH won't warn you; it just never finds a match.
How to add a default value#
Add one extra value after the last pair, with no option in front of it. SWITCH shows it whenever nothing matches:
=SWITCH(D2,"P","Pending","S","Shipped","D","Delivered","Check code")Now the R row shows "Check code" instead of #N/A. A default is good practice in almost every SWITCH formula, because new codes and typos turn up in real data. Make the default say something useful, so readers know the row needs a look.
Don't use IFERROR to hide the #N/A instead. A default handles the "no match" case on purpose, while IFERROR would also hide other problems. See IFERROR, used the right way for when wrapping makes sense.
How does SWITCH compare with IF and IFS?#
Use SWITCH when you check one cell against exact values. Use IFS or nested IF when the tests are ranges or different conditions. Here is the same code lookup written three ways:
=IF(D2="P","Pending",IF(D2="S","Shipped",IF(D2="D","Delivered","Check code")))
=IFS(D2="P","Pending",D2="S","Shipped",D2="D","Delivered",TRUE,"Check code")
=SWITCH(D2,"P","Pending","S","Shipped","D","Delivered","Check code")| Nested IF | IFS | SWITCH | |
|---|---|---|---|
| Repeats the cell | In every test | In every test | Once |
Handles ranges like >=90 | Yes | Yes | Not directly |
| "Otherwise" result | The last FALSE result | A final TRUE, pair | A single value at the end |
| Works in older Excel | Yes | Excel 2019 or later | Excel 2019 or later |
SWITCH is the shortest because the cell appears once. For grades, price bands and other ranges, read the nested IF guide for grades and bands or the IFS function guide. If plain IF is still new, start with the Excel IF function explained simply.
Can SWITCH handle ranges?#
There is a well-known workaround: switch on TRUE, and use tests as the options.
=SWITCH(TRUE,B2>=90,"A",B2>=80,"B",B2>=70,"C","F")SWITCH returns the result for the first test that equals TRUE. It works, but IFS says the same thing more plainly, so most people keep SWITCH for exact codes.
How to use SWITCH with numbers and other functions#
The expression can be a formula, not just a cell. A common example names days of the week from WEEKDAY, which returns 1 for Sunday through 7 for Saturday:
=SWITCH(WEEKDAY(B2),1,"Sunday",7,"Saturday","Weekday")For a date that falls on a Saturday, this gives "Saturday". For Monday to Friday it gives "Weekday", thanks to the default. Excel works out WEEKDAY once, then compares the result with each option.
When should you use a lookup table instead?#
Use a lookup table when the list is long or changes often. Ten or more codes in one formula are hard to read, and every new code means editing every copy of the formula.
Put the codes in one column and the labels in the next, then look them up:
=XLOOKUP(D2,$K$2:$K$12,$L$2:$L$12,"Check code")XLOOKUP needs Excel 2021 or Microsoft 365. The fourth argument does the same job as SWITCH's default. When a new code appears, you add a row to the table and every formula picks it up.
What are the common SWITCH mistakes?#
| Mistake | What you see | Fix |
|---|---|---|
| No default | #N/A for unknown codes | Add a final value, like "Check code" |
Text codes without quotes: P | #NAME? | Write "P" |
| Options and results swapped | The wrong labels | Option first, then its result |
| Extra space in the data, like "P " | The default or #N/A | Clean the data with TRIM |
| Number stored as text, like "1" | No match for 1 | Convert the codes to real numbers |
| Older Excel version | #NAME? | Use nested IF or a lookup table |
The last two data problems are easy to miss, because the cell looks right. Check suspicious cells with ISNUMBER, ISTEXT and ISBLANK, or use LEN to count the characters.
Practise it#
Try the priority exercise on this page, then practise SWITCH and the IS functions in a live spreadsheet. You'll check messy imported data with ISBLANK and ISNUMBER, then turn status codes into labels with SWITCH and add a default for a new code.
Try it yourself
This is a live spreadsheet. Type the formula in the green cell and press Enter.
Each ticket has a priority code: 1 = High, 2 = Medium, 3 = Low. In C2, use SWITCH on the code in B2 to show its label, and add Check as the default for any other code. Codes are numbers here, so they don't need quotes, but the labels do.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Priority | |||
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 | ||||
| 6 | ||||
| 7 | ||||
| 8 | ||||
| 9 |
Tip: Start with =SWITCH(B2, then each code and its label: 1,"High", and so on. The default goes last, on its own.
Practise this in a lesson
Frequently asked questions
What does the SWITCH function do in Excel?
SWITCH compares one value with a list of options and returns the result next to the first one that matches exactly. It replaces a chain of IF tests on the same cell.
How do I add a default value to SWITCH?
Add one extra value after the last pair, with no option in front of it: =SWITCH(D2,"P","Pending","S","Shipped","Check code"). It shows whenever nothing matches.
Why does SWITCH return #N/A?
The value didn't match any option, and the formula has no default. Add a default as the last argument, or check the data for typos and extra spaces.
Can SWITCH test ranges like greater than 90?
Not directly, because it only checks exact matches. A common workaround is =SWITCH(TRUE,B2>=90,"A",B2>=80,"B","C"), but IFS is clearer for ranges.
Which Excel versions have SWITCH?
SWITCH is in Excel 2019, Excel 2021, Excel 2024, Microsoft 365 and Excel for the web. Earlier versions show #NAME?, so use nested IF or a lookup table there.