Sub topic 2
SWITCH default values and unmatched input
This chapter assumes you can enter formulas, reference cells such as B2, use comparison operators, and write a basic IF formula.
SWITCH default values and unmatched input
SWITCH compares one expression to a list of exact values. Its syntax is SWITCH(expression, value1, result1, value2, result2, ..., default). The default is an optional last argument with no pair. SWITCH returns it when nothing matches.
=SWITCH(A2,"NY","New York","CA","California","TX","Texas","Unknown")If A2 holds "FL", no value matches, so the formula returns "Unknown". Without the default, the formula returns #N/A. Matching is exact, so a stray space in A2 ("NY ") will not match "NY".
SWITCH(TRUE, ...) as an alternative to IFS
Put TRUE in the expression slot and write conditions in the value slots. SWITCH returns the result paired with the first condition that is true.
=SWITCH(TRUE,B2>=90,"A",B2>=80,"B",B2>=70,"C","F")The default "F" works like the final TRUE fallback in IFS.
Choosing between IF, IFS, and SWITCH
| Function | Use it when |
|---|---|
| IF | One test with two outcomes |
| IFS | Several range tests, checked in order |
| SWITCH | One value matched against exact cases |
Reading common error values
| Error | Usual cause |
|---|---|
| #N/A | A lookup found no match, or SWITCH had no match and no default |
| #DIV/0! | A formula divides by zero or by an empty cell |
| #VALUE! | A value has the wrong type, such as text used in arithmetic |
| #REF! | A range reference is invalid, often after a row or column was deleted |
| #NAME? | Sheets does not recognize a function name, or text is missing its quotes |
| #NUM! | A number is invalid for the function, such as SQRT(-1) |
Exercise
Write a SWITCH formula that returns "Pass" when A2 is "yes" and "Fail" for any other value.
Show answer
=SWITCH(A2,"yes","Pass","Fail") Q: Why does =SWITCH(A2,"NY","New York") return #N/A when A2 is "CA"? A: No value matches "CA", and the formula has no default, so SWITCH returns #N/A.