Sub topic 6
SWITCH default values and unmatched input
Prerequisites and the matching model
This chapter assumes that you can enter Google Sheets formulas, use cell references such as A2, and write basic IF formulas. You should also know that a formula can return either a value or an error value.
SWITCH compares one expression with a list of exact case values. It returns the result attached to the first matching case. If no case matches, it returns a default value when you provide one.
=SWITCH(expression, case1, result1, case2, result2, default)The arguments come in pairs:
expressionis the value to test.- Each
caseis a value that might match the expression. - Each
resultis returned when its case matches. - The final unpaired argument is the default result.
For example, suppose A2 contains a state abbreviation:
=SWITCH(A2,"NY","New York","CA","California","TX","Texas","Unknown")If A2 contains CA, the formula returns California. If it contains WA, no case matches, so the formula returns Unknown.
Why the default matters
Input data often contains values that you did not expect. A user might enter a new code, leave a cell blank, or add an extra space. A default gives those values a deliberate result instead of an error.
| A2 input | Matching case | Result |
|---|---|---|
| `NY` | `NY` | `New York` |
| `TX` | `TX` | `Texas` |
| `WA` | None | `Unknown` |
| blank | None | `Unknown` |
Without the default, an unmatched expression causes SWITCH to return #N/A:
=SWITCH(A2,"NY","New York","CA","California")This formula works for NY and CA, but it returns #N/A for every other input. The error tells you that no case matched. That can be useful while checking data, but it may be confusing in a finished report.
Default values are not extra case pairs
A common mistake is forgetting that cases and results must be paired. In this formula, "Unknown" is the default because it has no following result:
=SWITCH(A2,"NY","New York","CA","California","Unknown")If you write another case, give it a result before adding the default:
=SWITCH(A2,"NY","New York","CA","California","TX","Texas","Unknown")You can use a blank default if the unmatched result should display nothing:
=SWITCH(A2,"NY","New York","CA","California","")However, a blank output can hide a data problem. Text such as "Check code" or "Unknown state" makes the issue visible.
The matching is exact. For example, "CA " with a trailing space may not match "CA". If the input may contain extra spaces, clean it before matching:
=SWITCH(TRIM(A2),"NY","New York","CA","California","Unknown")TRIM removes extra spaces from the beginning and end of the text.
See the decision path
Choosing a useful default
Choose the default based on what the sheet should do:
- Use
"Unknown"when the value is allowed but not mapped yet. - Use
"Check code"when the value probably indicates an input problem. - Use
""when unmatched values should be visually empty and are not important. - Use a numeric value such as
0only when zero has a clear meaning in the calculation.
If every possible input must be listed and unmatched values should be caught during testing, omit the default temporarily. Add a clear default before sharing the finished sheet.
Exercise
Write a SWITCH formula that returns Open for O, Closed for C, and Check status for every other value in A2.
Show answer
=SWITCH(A2,"O","Open","C","Closed","Check status")