Sub topic 4
Replacing #N/A with a Custom Result
This chapter assumes you can enter and copy formulas, use absolute references such as $E$2, and write a basic exact-match XLOOKUP. You need Excel for Microsoft 365, Excel 2021 or later, or Excel for the web.
Why a missing value returns #N/A
When XLOOKUP cannot find the lookup value, it returns the #N/A error. This is the "not available" error, and it means "no match exists." It is not a fault in the formula. A lookup against a customer list, for example, returns #N/A when the customer ID has not been entered yet.
The error causes two problems:
- It looks like a broken sheet to anyone reading it.
- It spreads. A formula such as
=SUM(F2:F20)returns#N/Aif any cell in the range holds#N/A.
The if_not_found argument
The fourth argument of XLOOKUP is if_not_found. XLOOKUP returns this value instead of #N/A when no match is found.
=XLOOKUP(E2, A2:A10, C2:C10, "Not found")If E2 appears in A2:A10, the formula returns the matching value from C2:C10 as usual. If it does not, the cell shows the text Not found.
The argument is optional. If you leave it out, a missing value still returns #N/A. The fourth position comes after the lookup array and return array, so the order is:
=XLOOKUP(lookup_value, lookup_array, return_array, if_not_found)What you can return
The if_not_found value can be any of the following:
| Value | Example | Result when no match |
|---|---|---|
| Text | `"Not found"` | The text appears |
| Number | `0` | A zero appears, which sums safely |
| Empty text | `""` | The cell looks blank |
| Another formula | `XLOOKUP(E2, H2:H10, I2:I10)` | A second lookup runs |
The last row is a fallback chain. If the first table has no match, XLOOKUP checks a second table.
=XLOOKUP(E2, A2:A10, C2:C10, XLOOKUP(E2, H2:H10, J2:J10, "Not in either list"))Excel evaluates the inner formula only when the outer lookup fails.
Choosing a good replacement value
Pick a replacement that suits what happens to the cell next.
- Text such as "Not found" works when people read the result. It does not work if other formulas do arithmetic on the cell. Adding text to a number returns
#VALUE!. - Zero works when the result feeds a total. It can hide the difference between a real zero and a missing record, so use it only when that difference does not matter.
- Empty text
""gives a clean look, but the cell still holds text, not a true blank. A formula such as=C2*2will return#VALUE!on it.
A caution about hiding errors
if_not_found replaces only the "no match" case. It does not catch other errors. If the return array contains an error, or the arguments are invalid, you still see that error. This is useful, because it means a real mistake is not buried.
Wrapping the whole formula in IFERROR is different. IFERROR hides every error, including mistakes such as a mistyped range. Prefer if_not_found for missing values and keep IFERROR for cases where you truly want to hide everything.
Worked example
A sheet lists product codes in A2:A10, product names in B2:B10 and prices in C2:C10. You type a code in E2 and want the price in F2.
- Enter
=XLOOKUP(E2, A2:A10, C2:C10, "Code not found")in F2. - Type a code that exists. F2 shows its price.
- Type a code that does not exist, such as
ZZZ. F2 showsCode not found. - Delete the fourth argument and retype
ZZZ. F2 shows#N/A.
Exercise
A sheet has order IDs in A2:A50 and order totals in D2:D50. Write a formula that looks up the ID in G2 and returns the total. If the ID is missing, the result must be 0 so that a later SUM still works.
Show answer
=XLOOKUP(G2, A2:A50, D2:D50, 0)
Exercise
Why is "Not found" a poor choice for ifnotfound when the result is later multiplied by a tax rate?
Show answer
Multiplying text by a number returns #VALUE!, so the error just moves to the next formula. A number such as 0 keeps the arithmetic working.