Sub topic 12
Built-in Handling with XLOOKUP
This chapter assumes you can enter and copy formulas, use relative and absolute references, and have used IF and VLOOKUP. Earlier chapters covered IFERROR, IFNA and the risk of masking real errors. Here you will see how XLOOKUP handles a missing value without a wrapper function.
Why XLOOKUP needs no wrapper
With VLOOKUP, a missing value returns #N/A, so you wrap the formula in IFNA to show a message instead. XLOOKUP has a fifth argument, if_not_found, that does this job itself.
=XLOOKUP(E2, $A$2:$A$100, $B$2:$B$100, "Not found")The arguments are:
lookup_value: the value to find (E2).lookup_array: the range to search ($A$2:$A$100).return_array: the range to return from ($B$2:$B$100).if_not_found: what to return when there is no match ("Not found").
If E2 is in column A, you get the matching value from column B. If it is not, you get the text "Not found".
What if_not_found does not catch
The if_not_found argument responds only to a missing match. It behaves like IFNA, not like IFERROR. Other problems still show as errors:
- A
#REF!from a deleted column in the return range still appears. - A
#NAME?from a misspelled function name still appears. - A
#VALUE!from return and lookup ranges of different sizes still appears.
This is the behavior you want. A missing key is an expected case, and the message covers it. A broken formula is a real fault, and it stays visible.
Comparing the options
The same task can be done several ways. The table shows the formulas side by side.
| Approach | Formula | Catches |
|---|---|---|
| IFERROR around VLOOKUP | `=IFERROR(VLOOKUP(E2, $A$2:$B$100, 2, FALSE), "Not found")` | Every error type |
| IFNA around VLOOKUP | `=IFNA(VLOOKUP(E2, $A$2:$B$100, 2, FALSE), "Not found")` | Only #N/A |
| XLOOKUP with if_not_found | `=XLOOKUP(E2, $A$2:$A$100, $B$2:$B$100, "Not found")` | Only a missing match |
XLOOKUP is shorter, and it defaults to an exact match, so you do not need the FALSE argument. It also searches by column, so inserting a column between A and B does not break the return range the way a hard-coded VLOOKUP column number can.
Chaining with XLOOKUP
Earlier you chained two lookups with nested IFNA. With XLOOKUP, the fallback goes straight into if_not_found.
=XLOOKUP(E2, Sheet1!$A$2:$A$100, Sheet1!$B$2:$B$100,
XLOOKUP(E2, Sheet2!$A$2:$A$100, Sheet2!$B$2:$B$100, "Not found"))Excel searches Sheet1 first. Only if that fails does it run the second XLOOKUP, which returns "Not found" when both sheets lack the value. You can add line breaks inside a formula in the formula bar with Alt+Enter, which keeps long formulas readable.
When to prevent instead
Sometimes you want a yes or no answer rather than a returned value. A preventive check with MATCH does this clearly.
=IF(ISNA(MATCH(E2, $A$2:$A$100, 0)), "Missing", "Found")MATCH returns a position or #N/A, and ISNA turns that into TRUE or FALSE. If you only need to know whether a value exists, COUNTIF($A$2:$A$100, E2) > 0 is another option. It does not use error handling at all.
Decision flow
The diagram shows how to choose for a lookup.
Common mistakes
- Using XLOOKUP in an older version. XLOOKUP is available in Microsoft 365 and Excel 2021 and later. In Excel 2019 and earlier it returns #NAME?, so use IFNA with VLOOKUP there.
- Mismatched range sizes. The lookup and return ranges must have the same number of rows. Otherwise you get #VALUE!, and
if_not_foundwill not hide it. - Treating the message as data. A returned "Not found" is text. A later SUM ignores it, but arithmetic such as
=F2*2produces #VALUE!. Pick a replacement value with this in mind.
Exercise
A price list is in A2:B100, with product codes in column A and prices in column B. Write one formula that looks up the code in E2 and returns 0 when the code is missing. Then explain why a #REF! caused by deleting column B would still be visible.
Show answer
=XLOOKUP(E2, $A$2:$A$100, $B$2:$B$100, 0). The ifnotfound argument responds only when no match exists. A deleted return column is a broken reference, not a missing match, so XLOOKUP still returns #REF!, which makes the fault visible.
Exercise
A colleague uses Excel 2016. Which formula from this chapter should they use to return "Not found" for a missing code, and why?
Show answer
=IFNA(VLOOKUP(E2, $A$2:$B$100, 2, FALSE), "Not found"). Excel 2016 has no XLOOKUP, and IFNA catches only #N/A, so other errors still show.