Conditional Logic

How to Use the SWITCH Function in Excel

Learn how the SWITCH function in Excel matches one value against a list of options, with a worked example, a default result, TRUE for ranges and versions.

On this page
  1. A worked example
  2. What happens with no default
  3. Matching a range with TRUE
  4. Numbers and text are different values
  5. When SWITCH is the wrong tool
  6. Versions

The SWITCH function compares one value against a list of possible values and returns the result paired with the first match. It suits cases such as turning department codes into names, where one value has several fixed options. SWITCH needs Excel 2019 or later, or Microsoft 365.

The syntax is:

=SWITCH(expression, value1, result1, value2, result2, ..., default)

The expression is the value to test. Each value and result pair is checked in order. The optional default at the end is returned when nothing matches.

A worked example

Suppose column A holds department codes, and you want the full department name in B2:

=SWITCH(A2, "SAL", "Sales", "OPS", "Operations", "FIN", "Finance", "Unknown")

If A2 holds SAL, the result is Sales. If A2 holds XYZ, which isn’t in the list, the result is Unknown. The final argument has no value paired with it, so Excel treats it as the default.

Compared with a nested IF that checks each code in turn, every SWITCH option sits in its own pair. Adding a code means adding one pair. How to write nested if formulas in excel covers the older approach.

What happens with no default

Without a default, SWITCH returns #N/A when no value matches. That error can be useful, because it shows that a code is missing from the list. If you would rather see a readable label, give the default a value such as “Unknown” or “Check code”.

Matching a range with TRUE

SWITCH matches exact values by default, so a range test needs a small change. Use TRUE as the expression, and put the conditions in the value positions:

=SWITCH(TRUE, A2>=90, "A", A2>=80, "B", A2>=70, "C", "F")

Excel tests each condition in order and returns the result for the first one that is TRUE. This is the same idea IFS uses, so the same rule applies: with “>=” thresholds, put the highest threshold first. For the grade example written both ways, see ifs function in excel vs nested if.

Numbers and text are different values

SWITCH compares values the same way an equals sign does, so the number 1 and the text “1” are not a match. If codes sometimes arrive as numbers and sometimes as text, a match can fail even though the codes look the same. How to convert text to number in excel covers the usual fix.

When SWITCH is the wrong tool

SWITCH works best with a short, fixed list. For a long list of codes that changes often, store the codes and names in a table and use a lookup. The formula then reads the name from the table, so updating a name doesn’t require editing every formula. How to use xlookup in excel shows the basic pattern.

Tests that are not exact matches, such as “greater than 500”, are usually clearer with IFS or a nested IF.

Versions

SWITCH is available in Excel 2019, Excel 2021, Microsoft 365 and Excel for the web. In older versions, a formula that uses it shows a #NAME? error. If a workbook will be shared with people on older versions, a nested IF or a lookup is the safer choice.

The complete guide to conditional logic

How to Use the IF Function in Excel
Read the guide

Keep exploring

Guides from other topics

All topics →

How to Look Up Data in Excel

How lookups work in Excel, with VLOOKUP, XLOOKUP and INDEX with MATCH, how to set up your data, and how to choose the right function for the job.

Lookups 3 min read