Sub topic 5
Common errors and data-type pitfalls
This chapter assumes you can write a basic XLOOKUP with an exact match and a custom if_not_found message. It covers why a formula that looks correct still returns an error or a wrong result, and how to fix each case.
Reading the error
Each error value points to a different kind of problem.
| Result | Usual cause | First thing to check |
|---|---|---|
| `#N/A` | No match was found and `if_not_found` is not set | Spelling, spaces, and data types |
| `#VALUE!` | The lookup and return arrays have different sizes, or the lookup array is not a single row or column | Range sizes |
| `#SPILL!` | A multi-value result cannot spread into its neighbors | Cells blocking the spill range |
| `#REF!` | A referenced range was deleted, or points to a closed workbook | The cell references |
| `#NAME?` | The Excel version does not support XLOOKUP | Your Excel version |
Text that looks like numbers
The most common cause of #N/A is a data-type mismatch. Excel treats the number 1001 and the text "1001" as different values, even though they look the same in a cell. Text numbers often come from imported files or IDs with leading zeros. They usually appear left-aligned, and they sometimes show a green triangle in the corner.
If the lookup value is text and the lookup column holds numbers, convert the lookup value:
=XLOOKUP(VALUE(E2), A2:A10, C2:C10, "Not found")If the lookup column holds text and the lookup value is a number, go the other way. Joining an empty string turns the number into text:
=XLOOKUP(E2&"", A2:A10, C2:C10, "Not found")Converting the lookup value is usually safer than editing the source data, because the source may be refreshed later.
Hidden spaces and case
A cell containing "Smith " with a trailing space does not match "Smith". TRIM removes extra spaces. Web data often contains non-breaking spaces, which TRIM ignores, so replace those first with SUBSTITUTE:
=XLOOKUP(TRIM(SUBSTITUTE(E2, CHAR(160), " ")), A2:A10, C2:C10, "Not found")This cleans the lookup value only. If the spaces are in the lookup column, clean the column itself, for example in a helper column.
XLOOKUP ignores letter case, so "abc" and "ABC" match. That is rarely a problem, but it matters if your keys differ only by case.
Mismatched range sizes
The lookup array and return array must line up. Here the return range is one row short:
=XLOOKUP(E2, A2:A10, C2:C9)This returns #VALUE!. Both ranges must cover the same rows (or the same columns, for a horizontal lookup). The mismatch often appears after you insert rows or copy a formula and a relative reference shifts. Use absolute references such as $A$2:$A$10 when you copy the formula down.
The lookup array must also be one row or one column. A range such as A2:B10 as the lookup array produces #VALUE!.
Spill errors
A formula that returns several values, such as =XLOOKUP(E2, A2:A10, B2:D10), needs empty cells to the right. If any of those cells holds data, you get #SPILL!. Clear the blocking cells and the result appears. Merged cells and Excel tables can also block spilling, because tables do not allow spilled formulas.
Not found versus broken
if_not_found replaces only the "no match" result. It does not hide #VALUE! or #REF!, which is useful, because those errors signal a real problem. Wrapping the formula in IFERROR hides every error, including a mistyped range. Prefer if_not_found when you only want to handle missing values.
Wildcards in exact mode
In the default match_mode of 0, characters such as * and ? are ordinary text. Looking up "Sm*" finds only a cell that contains exactly Sm*. If you want wildcard behavior, set match_mode to 2, as covered in a later chapter.
Exercise
A lookup in column A contains the numbers 101, 102 and 103 stored as text. Cell E2 holds the number 102, and =XLOOKUP(E2, A2:A4, B2:B4) returns #N/A. Write a formula that fixes the problem without changing column A.
Show answer
Convert the lookup value to text: =XLOOKUP(E2&"", A2:A4, B2:B4). The number 102 becomes the text "102", which matches the text values in column A.