Sub topic 6
Handling keys that are not found
This chapter assumes you can enter formulas in Google Sheets and read ranges such as A2:C100. It also assumes you know absolute references with $ and have already written an exact-match VLOOKUP and a basic XLOOKUP. The goal here is to control what a lookup shows when the key is missing, and to avoid hiding problems that you should see.
Why a missing key produces an error
A lookup compares your key against every value in the lookup column. If no value matches, there is nothing to return. Google Sheets reports this with the #N/A error, which means "not available."
An exact-match lookup is the usual place to see this. Take this product table in A1:C5:
| Row | A | B | C |
|---|---|---|---|
| 1 | SKU | Name | Price |
| 2 | P-101 | Bolt | 0.25 |
| 3 | P-102 | Widget | 4.50 |
| 4 | P-103 | Gasket | 1.10 |
| 5 | P-104 | Hinge | 2.75 |
If E2 holds P-999, this formula returns #N/A:
=VLOOKUP(E2, A2:C5, 3, FALSE)The error is correct. The key does not exist. The problem begins when the error spreads. Any formula that depends on the lookup, such as a total or a multiplication, also returns #N/A, and one missing key can break a whole report.
You have two jobs: decide what a missing key should look like, and make sure that a key which should be found does not get reported as missing (covered at the end of this chapter).
The missing_value argument in XLOOKUP
XLOOKUP has a built-in way to handle a missing key. Its fourth argument is missing_value. If the key is not found, XLOOKUP returns this value in place of #N/A.
=XLOOKUP(E2, A2:A100, C2:C100, "Not found")The arguments, in order:
E2is the search key.A2:A100is the lookup range.C2:C100is the result range."Not found"is returned when there is no match.
The missing value can be text, a number, or a cell reference. Choose it to suit what happens downstream:
- Text such as
"Not found"is clear to a human reader. If a formula multiplies this cell by a quantity, the result is a#VALUE!error, because text cannot be multiplied. 0keeps arithmetic working. It also makes a missing price look like a free item, which is a risk.- An empty string
""leaves the cell looking blank. Arithmetic on it still fails in Sheets, and a blank can hide a real problem.
The right choice depends on who reads the sheet and what formulas use the cell.
=XLOOKUP(E2, A2:A100, C2:C100, 0)This version returns 0 for a missing SKU. Use it only when zero is a safe stand-in.
The argument order matters
missing_value is the fourth argument, so it comes after the result range and before the match mode. If you also want to set a match mode, you must supply the fourth argument first:
=XLOOKUP(B2, $F$2:$F$6, $G$2:$G$6, "Below minimum", -1)Here "Below minimum" appears when B2 is smaller than every value in the tier table. The -1 is the match mode, covered in a later chapter. You cannot skip the fourth argument to reach the fifth. If you want the default #N/A behavior and still need a match mode, leave the fourth argument empty with two commas:
=XLOOKUP(B2, $F$2:$F$6, $G$2:$G$6, , -1)IFNA for VLOOKUP
VLOOKUP has no missing_value argument. To replace its #N/A, wrap it in IFNA.
=IFNA(VLOOKUP(E2, A2:C100, 3, FALSE), "Not found")IFNA(value, value_if_na) evaluates the first argument. If the result is #N/A, it returns the second argument. Otherwise it returns the first argument unchanged.
IFNA works with any formula, so you can also wrap XLOOKUP in it. The XLOOKUP form with missing_value is shorter and easier to read, so prefer it when you use XLOOKUP.
flowchart TD
A[Lookup formula runs] --> B{Key found?}
B -- Yes --> C[Return matching value]
B -- No --> D{Fallback set?}
D -- Yes --> E[Return fallback value]
D -- No --> F[Return #N/A]The diagram above is for orientation only. The version Sheets renders is below.
IFNA versus IFERROR
IFERROR also replaces errors, but it catches every error type: #N/A, #REF!, #VALUE!, #DIV/0! and #NAME?. That is a problem for lookups, because it hides mistakes in your own formula.
Suppose you misspell a function name inside a wrapped formula:
=IFERROR(VLOKUP(E2, A2:C100, 3, FALSE), "Not found")VLOKUP is not a function, so the inner formula returns #NAME?. IFERROR swallows it and shows "Not found" for every row. The sheet looks fine, and every answer is wrong.
IFNA catches only #N/A, so the misspelling would still show #NAME? and you would notice. Likewise, XLOOKUP's missing_value applies only to a key that is not found. Other errors still surface.
| Approach | Catches | Best use |
|---|---|---|
| `XLOOKUP(..., "Not found")` | Only a missing key | XLOOKUP formulas |
| `IFNA(VLOOKUP(...), "Not found")` | Only `#N/A` | VLOOKUP formulas |
| `IFERROR(..., "Not found")` | Every error type | Rarely, for lookups |
Choosing what to show
A fallback should help the next person act. Compare these:
"Not found"tells a reader the lookup ran and failed."Check SKU"tells a reader what to do.""shows nothing and gives no hint.0can pass as real data.
A useful pattern is to return the key itself inside a message. This is handy in a long list, because you can see which key failed without checking the neighboring column:
=XLOOKUP(E2, A2:A100, C2:C100, "No match for " & E2)If E2 is P-999, the cell shows No match for P-999. XLOOKUP evaluates missing_value as an ordinary expression, so concatenation with & works.
A fallback can also be another lookup. This searches a main table first, then a backup table:
=XLOOKUP(E2, A2:A100, C2:C100, XLOOKUP(E2, H2:H100, J2:J100, "Not found"))The inner XLOOKUP is the missing_value of the outer one. The backup search is only used when the first one fails.
Counting and flagging missing keys
Sometimes you want to find the misses and not just hide them. Return a clear marker, then count or filter on it.
=XLOOKUP(E2, A2:A100, C2:C100, "MISSING")With that in column F, this counts the misses:
=COUNTIF(F2:F500, "MISSING")You can also test whether a key exists without returning data, using COUNTIF:
=IF(COUNTIF(A2:A100, E2) = 0, "Not found", "OK")Note that COUNTIF treats characters such as * and ? in the criterion as wildcards, so keys that contain them can give surprising counts.
When the key exists but is reported missing
A fallback message that appears for a key you can see in the table is a symptom. Do not paper over it. The usual causes are:
- Hidden spaces.
"P-102 "with a trailing space does not equal"P-102". Wrap the key inTRIM, as inVLOOKUP(TRIM(E2), A2:C100, 3, FALSE). A non-breaking space, common in pasted web data, is not removed byTRIM. UseSUBSTITUTE(E2, CHAR(160), "")for that case. - Numbers stored as text. The text
"102"does not equal the number102. Check by comparingISNUMBER(E2)withISNUMBER(A2). Convert withVALUE()orE2 * 1. - Wrong range. In VLOOKUP, the search key must be in the first column of the range. If your range starts at column B but the keys are in A, every lookup fails.
- Unlocked ranges. When you copy a formula down,
A2:C100becomesA3:C101, thenA4:C102, and the table drifts away from its keys. Write$A$2:$C$100. - Approximate match left on. A VLOOKUP without
FALSEcan return a wrong row, or#N/A, when the data is unsorted.
Here is a combined, defensive form:
=XLOOKUP(TRIM(E2), $A$2:$A$100, $C$2:$C$100, "No match for " & E2)A fallback is meant for keys that are truly absent. If you find a fallback message for a key you know is there, fix the data or the formula before you rely on the result.
Summary of the options
| Goal | Formula pattern |
|---|---|
| Replace a missing key in XLOOKUP | `XLOOKUP(key, find, return, "text")` |
| Replace `#N/A` in VLOOKUP | `IFNA(VLOOKUP(...), "text")` |
| Keep the default error but set a match mode | `XLOOKUP(key, find, return, , mode)` |
| Show which key failed | `"No match for " & key` as the fallback |
| Try a backup table | Nest a second XLOOKUP as `missing_value` |
Exercise
Column A2:A50 holds employee IDs and column D2:D50 holds departments. Cell F2 holds an ID typed by a user, and it may have a trailing space. Write one formula that returns the department, and returns the text "Unknown ID" when the ID is not in the list. Then explain why IFERROR is a poor choice for this task.
Show answer
=XLOOKUP(TRIM(F2), $A$2:$A$50, $D$2:$D$50, "Unknown ID"). TRIM removes the trailing space, and the fourth argument supplies the fallback. IFERROR is a poor choice because it hides every error type, so a typo in a function name or a broken range would also show "Unknown ID" and the real fault would go unnoticed. The fourth argument of XLOOKUP, or IFNA with VLOOKUP, replaces only the not-found case.
Exercise
This formula shows "Not found" for the SKU P-102, which is clearly in the table: =IFNA(VLOOKUP(E2, A2:C100, 3), "Not found"). Name two likely causes you would check first.
Show answer
First, the last argument is missing, so VLOOKUP uses approximate match. On unsorted data that can skip the right row and return #N/A, which IFNA then turns into "Not found". Adding FALSE fixes this. Second, the key or the table entry may contain a hidden space or be a number stored as text, so the values look equal but are not. Check with TRIM, and compare ISNUMBER on both cells.