Sub topic 6
Handling Keys That Are Not Found
Assume you know how to enter formulas in Google Sheets, use cell and range references, apply absolute references such as $A$2:$C$100, and reference another sheet. This chapter focuses on what happens when a lookup key does not appear in the lookup range.
A lookup uses a key, such as a product ID, to find a row. It then returns related data from that row. For example, this formula searches for the key in E2:
=XLOOKUP(E2, A2:A100, C2:C100)If E2 contains P-102 and that key exists in A2:A100, Google Sheets returns the corresponding value from C2:C100. If the key is absent, the formula returns #N/A.
Why `#N/A` appears
#N/A means that a formula could not find a matching value. It is different from a blank result. A matching row might contain an empty cell, while #N/A means no matching row was found.
Common causes include:
- The key really is missing.
- The key contains an extra leading or trailing space.
- One key is stored as text, while the other is stored as a number.
- The lookup and result ranges have different sizes.
- The formula uses an exact match, but the values are not identical.
The following flow shows the decision point:
Use XLOOKUP's missing value argument
XLOOKUP accepts a fourth argument named missing_value. Put the message you want to display in that position:
=XLOOKUP(E2, A2:A100, C2:C100, "Not found")The arguments are:
| Argument | Purpose |
|---|---|
| `E2` | Key to search for |
| `A2:A100` | Lookup range |
| `C2:C100` | Result range |
| `"Not found"` | Value returned when no key matches |
This keeps the result readable and prevents a visible #N/A from confusing users of the sheet. You can also return an empty string if a missing key should display as a blank:
=XLOOKUP(E2, A2:A100, C2:C100, "")Use a descriptive message when missing data needs attention. Use an empty string only when a blank is meaningful and users have another way to identify missing records.
Handle VLOOKUP errors with IFNA
VLOOKUP does not have a dedicated missing-value argument. Wrap it in IFNA, which replaces an #N/A result with the value you specify:
=IFNA(VLOOKUP(E2, A2:C100, 3, FALSE), "Not found")FALSE requests an exact match. The IFNA function changes only the #N/A error. This is useful because it preserves other errors that may reveal a broken formula or invalid range.
You can also use IFNA with a direct lookup:
=IFNA(VLOOKUP("P-102", A2:C100, 3, FALSE), "Product ID missing")Distinguish missing keys from dirty keys
Before changing the formula, inspect the data. A hidden space can make two values look identical while remaining different to a lookup.
Clean the key before searching:
=XLOOKUP(TRIM(E2), A2:A100, C2:C100, "Not found")TRIM removes leading and trailing spaces from E2. If the spaces are in the lookup column itself, clean that source column or use a helper column containing trimmed values.
For numbers stored as text, convert the search key when appropriate:
=XLOOKUP(VALUE(E2), A2:A100, C2:C100, "Not found")Use VALUE only when E2 should contain a number. Product IDs such as 00102 may need to remain text because converting them to numbers removes meaningful leading zeros.
Exercise
Write a formula that looks up the key in E2 from A2:A100, returns the matching value from C2:C100, and displays "Product missing" when no match exists.
Show answer
=XLOOKUP(E2, A2:A100, C2:C100, "Product missing")