Sub topic 4
Replacing #N/A with a custom result
This chapter assumes you can enter and copy formulas, you understand absolute references such as $E$2, and you have Excel for Microsoft 365, Excel 2021 or later, or Excel for the web. It also assumes you know the first three XLOOKUP arguments from the earlier chapters: lookup_value, lookup_array and return_array.
Why a missing value produces #N/A
When XLOOKUP cannot find the lookup value in the lookup array, it returns #N/A. The name stands for "not available". That is a correct answer, because there is nothing to return. It is a poor thing to show in a report, though. A single #N/A in a column also breaks any SUM, AVERAGE or arithmetic that refers to that cell.
XLOOKUP has a built-in way to handle this. The fourth argument, if_not_found, holds the result to return when there is no match. You don't need to wrap the formula in another function.
The if_not_found argument
Here is the argument list again, with the fourth argument highlighted by its position:
=XLOOKUP(lookup_value, lookup_array, return_array, [if_not_found], [match_mode], [search_mode])Square brackets mean the argument is optional. If you leave out if_not_found, a failed lookup returns #N/A. If you supply it, XLOOKUP returns that value instead.
The basic form from this tutorial's examples is:
=XLOOKUP(E2, A2:A10, C2:C10, "Not found")This formula looks for the value in E2 within A2:A10. If it finds a match, it returns the value from the same position in C2:C10. If it finds none, the cell shows the text Not found.
A worked example
Suppose A2:A10 holds product codes, B2:B10 holds product names, and C2:C10 holds prices. A user types a code in E2, and the price appears in F2.
| Cell range | Contents |
|---|---|
| A2:A10 | Product codes, such as `P-100`, `P-101` |
| B2:B10 | Product names |
| C2:C10 | Prices |
| E2 | The code the user types |
| F2 | The formula |
Put this formula in F2:
=XLOOKUP(E2, A2:A10, C2:C10, "Not found")Typing P-101 returns that product's price. Typing P-999, which isn't in the list, returns Not found. Without the fourth argument, the second case would show #N/A.
Choosing what to return
The if_not_found argument accepts any value or expression, not just text. Pick the fallback based on how the result is used next.
Text for people
When the cell is read by a person, a short message is clear. Including the lookup value helps them see which input failed:
=XLOOKUP(E2, A2:A10, C2:C10, "No match for " & E2)If E2 contains P-999, the result is No match for P-999. Because the argument is an expression, XLOOKUP evaluates it like any other formula.
Zero for numbers
When other formulas add or multiply the result, return a number:
=XLOOKUP(E2, A2:A10, C2:C10, 0)Text such as "Not found" in a price column makes =F2*G2 fail with #VALUE!. A 0 keeps the arithmetic working. Be aware that 0 is also a legitimate price, so a reader can't tell a missing product from a free one. If that distinction matters, use a message and handle it downstream.
An empty string for a blank-looking cell
An empty pair of quotes returns empty text, so the cell looks blank:
=XLOOKUP(E2, A2:A10, C2:C10, "")This is tidy for display, but the cell is not truly empty. It holds a zero-length text value. Arithmetic on it produces #VALUE!, and ISBLANK returns FALSE for it. Use "" only when the result is shown, not calculated.
A cell reference
You can point to a cell so the message is editable without touching the formula:
=XLOOKUP(E2, A2:A10, C2:C10, $H$1)If H1 contains Check the product code, that text appears for every failed lookup. Changing H1 updates every formula that uses it. The absolute reference keeps the pointer fixed when you copy the formula down.
Falling back to a second lookup
Because the fourth argument can be any expression, it can be another XLOOKUP. This lets you search a second table when the first one fails:
=XLOOKUP(E2, A2:A10, C2:C10, XLOOKUP(E2, G2:G10, H2:H10, "Not found"))Excel searches A2:A10 first. Only if that fails does it search G2:G10. If both fail, you get Not found. This pattern suits a current price list plus an archive list. You can nest further, but each level makes the formula harder to read, so past two levels consider a helper column or a combined table.
Skipping the argument
If you want the default #N/A but need to set a later argument such as match_mode, leave the fourth position empty with consecutive commas:
=XLOOKUP(B2, $E$2:$E$6, $F$2:$F$6, , -1)Here the empty slot means a failed lookup still returns #N/A. Writing "" instead of nothing would change the behavior to returning empty text. The two forms look similar, so check the commas carefully.
Multi-column returns
If the return array has several columns, a successful lookup spills the whole matching row:
=XLOOKUP(E2, A2:A10, B2:D10, "Not found")On a match, the values from B, C and D spill across three cells. On a miss, the fallback is a single value, so you get one Not found in the first cell and nothing in the cells beside it. If the layout must stay aligned, put the formula where a one-cell result is acceptable, or return a fallback built for the shape you need. Test the miss case on your own sheet, because the spill area changes size between the two outcomes. A change in size can cause a #SPILL! error if other data sits in the cells the match would need.
What if_not_found does not catch
The fourth argument handles one situation: the lookup value is absent from the lookup array. It does not replace other errors.
| Situation | Result with `if_not_found` set |
|---|---|
| Lookup value not in the lookup array | The fallback value |
| Lookup and return arrays have different sizes | `#VALUE!` |
| A reference points to a deleted range | `#REF!` |
| The matched cell in the return array itself contains `#N/A` | `#N/A` is returned as it is |
| The lookup value is itself an error such as `#N/A` | The error passes through |
The last two rows surprise people. If the matching row's return cell holds an error from some earlier formula, XLOOKUP found the match and returns what is there. The fallback is not used.
Comparison with IFNA and IFERROR
Older workbooks wrap lookups in IFERROR or IFNA. For XLOOKUP, the built-in argument is usually better.
=IFNA(XLOOKUP(E2, A2:A10, C2:C10), "Not found")
=IFERROR(XLOOKUP(E2, A2:A10, C2:C10), "Not found")| Approach | Catches missing values | Catches other errors | Notes |
|---|---|---|---|
| `if_not_found` argument | Yes | No | Shortest, and the lookup value is only evaluated once |
| `IFNA(...)` | Yes | Only `#N/A`, including one in the return array | Also hides `#N/A` from the data itself |
| `IFERROR(...)` | Yes | All errors | Hides real mistakes such as a wrong range |
IFERROR is the riskiest choice. If you mistype a range and get #REF! or #VALUE!, it shows Not found, and the real fault stays hidden. With if_not_found, those errors stay visible so you can fix them.
A caution about hiding real problems
A friendly fallback message can hide data faults. Two common causes of a miss are not "the item doesn't exist":
- A code stored as text in one range and as a number in the other. The two look identical but never match.
- Extra spaces, such as
"P-101 "with a trailing space.
With a plain #N/A, the error draws attention. With "Not found", a whole column of failures can look like normal results. When you first build a lookup, leave if_not_found out and confirm the formula returns real values. Add the fallback after that. The next chapter covers diagnosing these data-type problems in detail.
Exercises
Exercise
A sheet has employee IDs in A2:A50 and departments in C2:C50. Write a formula for F2 that looks up the ID in E2 and shows "Unknown ID" when it is absent.
Show answer
=XLOOKUP(E2, A2:A50, C2:C50, "Unknown ID")
Exercise
Your formula is =XLOOKUP(E2, A2:A10, C2:C10, ""), and another cell multiplies its result by 1.2. For a missing ID, the multiplication shows #VALUE!. Why, and what is a fix?
Show answer
The empty string is text, not a number, so multiplying it fails. Return a number instead, for example =XLOOKUP(E2, A2:A10, C2:C10, 0), or test the result before multiplying.
Exercise
You want a lookup in A2:A10 and C2:C10 to return #N/A on a miss, but you also need matchmode -1. How do you write the formula without setting ifnot_found?
Show answer
Leave the fourth argument empty with two commas: =XLOOKUP(E2, A2:A10, C2:C10, , -1)
Exercise
A formula uses =IFERROR(XLOOKUP(E2, A2:A10, C2:C5), "Not found"). The return range is shorter than the lookup range. What does the user see, and why is this a problem?
Show answer
The mismatched sizes cause #VALUE!, and IFERROR turns it into "Not found". The user sees a normal-looking miss message while the real fault, a wrong range, stays hidden. Using the ifnotfound argument instead would leave the #VALUE! error visible.