XLOOKUP & INDEX-MATCH Data Cleaning Guide
Merge research tables, student rosters, and financial exports cleanly by stripping invisible characters with TRIM and CLEAN and writing rock-solid exact lookups with XLOOKUP or INDEX-MATCH.
=XLOOKUP(TRIM(CLEAN(A2)), KeyCol, ValCol, "Not Found", 0) | =INDEX(ValCol, MATCH(A2, KeyCol, 0))
1. Cleaning Dirty Keys Before Running Lookups
The #1 reason a valid lookup returns #N/A is invisible formatting differences between two imported tables (such as a trailing space in "STU-104 " vs. "STU-104", or numeric IDs stored as text). Clean keys systematically:
- Strip extra spaces & control characters:
=TRIM(CLEAN(A2))removes leading/trailing spaces, collapses double interior spaces to one, and deletes ASCII 0–31 line breaks/control codes. - Remove web non-breaking spaces (HTML
):TRIMdoes not remove Unicode character 160! For web-copied tables, use=TRIM(SUBSTITUTE(CLEAN(A2), CHAR(160), " ")). - Fix Text-vs-Number ID mismatches: If Table 1 stores student ID
10425as a number and Table 2 stores"10425"as text (green corner triangle), coerce them to match using=VALUE(A2)(to number) or=A2&""(to text). - Standardize casing for visual consistency: While Excel lookups are case-insensitive, wrapping labels in
UPPER()orPROPER()keeps PivotTables and exported CSVs tidy.
2. XLOOKUP vs. INDEX-MATCH vs. VLOOKUP
| Feature | XLOOKUP (Excel 365 / 2021+) |
INDEX-MATCH (All Versions) |
VLOOKUP (Legacy) |
|---|---|---|---|
| Default Match Mode | Exact match (0) by default | Must specify 0 in MATCH |
Approximate by default (dangerous unless FALSE is added) |
| Look Left of Key Column? | Yes | Yes | No (key must be leftmost column) |
| Survives Column Insertions? | Yes (separate arrays) | Yes (separate ranges) | Breaks hardcoded col_index_num |
| Built-in Not-Found Message | Yes (4th argument, e.g., "Missing") |
Needs IFERROR(..., "Missing") |
Needs IFERROR(..., "Missing") |
3. Worked Examples (Including Two-Criteria Lookups)
Example A: Merging Student IDs with Scores (XLOOKUP & INDEX-MATCH)
Suppose Sheet1 has a messy Student ID in A2 (" ID-204 "), and Sheet2 stores clean IDs in $A$2:$A$500 and Midterm Scores in $D$2:$D$500.
- Modern Excel (XLOOKUP):
=XLOOKUP(TRIM(A2), Sheet2!$A$2:$A$500, Sheet2!$D$2:$D$500, "No Score", 0) - Universal Excel (INDEX-MATCH):
=IFERROR(INDEX(Sheet2!$D$2:$D$500, MATCH(TRIM(A2), Sheet2!$A$2:$A$500, 0)), "No Score")
Example B: Multi-Criteria Lookup (Matching Both Course AND Section)
To look up an instructor where Course (A2) matches $E$2:$E$100 AND Section (B2) matches $F$2:$F$100, concatenate both keys with a delimiter:
=XLOOKUP(A2&"|"&B2, $E$2:$E$100&"|"&$F$2:$F$100, $G$2:$G$100, "Not Found")
Using a pipe separator "|" prevents accidental collisions like "1" & "23" matching "12" & "3".
$A$2:$A$500) before dragging a lookup formula down shifts the lookup table by one row per cell and causes false #N/A errors halfway down the sheet. (2) Always verify invisible spaces with =LEN(A2) vs. =LEN(Sheet2!A2) when two cells look identical on screen but fail to match.
Frequently Asked Questions
Why does my lookup return #N/A even though the ID is clearly in the table?
Check three culprits in order: (1) trailing spaces or CHAR(160) non-breaking spaces (check with =LEN()), (2) one cell is stored as Text while the other is a Number (test with =ISTEXT(A2)), or (3) your range wasn’t locked with $ signs when copied down.
What if a key appears multiple times in the lookup column?
By default, XLOOKUP and INDEX-MATCH return the first matching row from the top. With XLOOKUP, you can set the 6th argument (search_mode) to -1 to return the last (most recent) match instead.
Should I use XLOOKUP or INDEX-MATCH if sharing workbooks with older Excel users?
Use INDEX-MATCH if collaborators run Excel 2016 or 2019 (where XLOOKUP shows _xlfn.XLOOKUP errors). In Microsoft 365, Excel 2021+, and Google Sheets, XLOOKUP is supported natively.