Sub topic 12
Troubleshooting #N/A results
This chapter assumes you can enter formulas in Google Sheets, read ranges such as A2:C100, use absolute references with $, and reference another sheet. It also assumes you know the basic shape of VLOOKUP and XLOOKUP from earlier chapters.
A lookup returns #N/A when it cannot find the key. The message means "not available". Most of the time the key really is absent from the data. Often, though, the key looks present and the lookup still fails. This chapter shows how to tell the two cases apart and how to fix the second one.
What #N/A tells you and what it does not
#N/A says only that no match was found. It does not say why. The cause is one of these:
- The key is not in the lookup column at all.
- The key is there, but it differs from the search key in a way you cannot see, such as a space.
- The key and the lookup column hold different data types, such as the number
102and the text"102". - The lookup range is wrong, for example it does not start at the key column or it is shifted by one row.
- Approximate match is in use on data that is not sorted.
Other errors point to other problems. #REF! from VLOOKUP means the column index is larger than the number of columns in the range. #VALUE! often means a range argument has the wrong shape. Do not treat all errors as #N/A problems.
A diagnostic routine
Work through the same steps each time. They go from cheapest to most involved.
Step 1: Confirm the key exists
Use COUNTIF to count how many times the key appears in the lookup column.
=COUNTIF(A2:A100, E2)A result of 0 means the lookup is probably correct, and the key is missing. A result of 1 or more means the lookup should succeed, so something else is wrong. Note that COUNTIF is more forgiving than an exact lookup in one respect: it matches text case-insensitively and can treat a numeric-looking criterion as a number. A count above zero is a strong hint, not proof.
Step 2: Compare the cells directly
Put the search key in one cell and a visible copy of the target in another. Then test equality:
=E2=A5FALSE for two cells that look identical confirms a hidden difference. TRUE means the problem is in the lookup formula, not the data.
Hidden spaces
Spaces are the most common cause of a false #N/A. Data pasted from web pages, exports, and other systems often carries a leading space, a trailing space, or a doubled space inside the text. "P-102 " does not equal "P-102".
Measure the length of both cells to find out:
=LEN(E2)
=LEN(A5)If the visible text is P-102 and LEN returns 6, one character is hidden. Compare the numbers for the key and the table entry to see which side carries it.
TRIM removes leading and trailing spaces and collapses repeated interior spaces to one:
=VLOOKUP(TRIM(A2), Products!A2:C100, 3, FALSE)This cleans the search key. If the spaces are in the lookup column instead, you cannot wrap the whole column in TRIM inside VLOOKUP, because VLOOKUP needs a real range. XLOOKUP allows it, since its lookup argument can be an array expression. Use ARRAYFORMULA to apply TRIM to the whole column:
=XLOOKUP(TRIM(E2), ARRAYFORMULA(TRIM(A2:A100)), C2:C100, "Not found")This works but recalculates the trimmed column on every evaluation. For a large table, clean the source data once instead (see "Fixing the data at its source" below).
Characters that TRIM does not remove
TRIM handles only the ordinary space character (code 32). Web data often contains a non-breaking space (code 160), which looks the same but is a different character. TRIM leaves it in place. Replace it first:
=TRIM(SUBSTITUTE(A2, CHAR(160), " "))Non-printing characters such as line breaks are removed by CLEAN. A combined cleanup looks like this:
=TRIM(CLEAN(SUBSTITUTE(A2, CHAR(160), " ")))To find out which unusual character is present, inspect a suspect position with UNICODE:
=UNICODE(RIGHT(A5, 1))A value of 32 is an ordinary space. 160 is a non-breaking space. Other values point to other invisible characters.
Numbers stored as text
Google Sheets distinguishes the number 102 from the text "102". A lookup treats them as different values. This happens when IDs are imported from a CSV file, when a cell has a leading apostrophe, or when a column is formatted as plain text before data is pasted in.
Signs of text numbers:
- The value is left-aligned in a cell with default alignment, while real numbers are right-aligned.
ISNUMBERreturnsFALSEfor a cell that looks numeric.- Leading zeros survive in a code such as
00123, which suggests text.
Test both sides:
=ISNUMBER(E2)
=ISNUMBER(A5)If one returns TRUE and the other FALSE, the types disagree. Convert the side you can change.
To turn a text number into a real number, use VALUE, or multiply by one:
=VLOOKUP(VALUE(E2), A2:C100, 3, FALSE)To go the other way, join an empty string to the number. This produces text:
=VLOOKUP(E2&"", A2:C100, 3, FALSE)Match the direction to the data you want to keep. If the lookup column holds text IDs with leading zeros, convert the search key to text and keep the column as it is. Converting the column to numbers would drop the zeros and change the identifiers.
Be careful with VALUE when the key may already be a number or may contain spaces. VALUE(" 102") can fail or behave differently from VALUE("102"). Trim first when in doubt:
=VLOOKUP(VALUE(TRIM(E2)), A2:C100, 3, FALSE)Unsorted data and approximate match
VLOOKUP with TRUE, or with the last argument omitted, uses approximate match. It assumes the first column is sorted ascending and uses a binary search. On unsorted data the search can skip over a key that is present and return #N/A or, worse, a wrong value with no error.
=VLOOKUP(B2, $F$2:$G$6, 2, TRUE)Two fixes exist:
- For identifier lookups, use
FALSEso that only an exact match counts. - For a tier table, sort the first column ascending, or use
XLOOKUPwith a match mode, which does not require sorting.
=XLOOKUP(B2, $F$2:$F$6, $G$2:$G$6, "Below minimum", -1)An approximate lookup returns #N/A when the search key is smaller than every value in the table. This is correct behavior, because no tier applies. Handle it with the missing_value argument rather than hiding it.
Range mistakes
A lookup can fail because the range is not what you think it is.
- Key column not first.
VLOOKUPsearches only the first column of its range. If the range isB2:D100but the key is in column A, every lookup fails. Move the range start to the key column, or switch toXLOOKUP. - Missing anchors. When you fill a formula down, relative references shift. A range written as
A2:C100becomesA3:C101in the next row, and the table slides away from the data. Write$A$2:$C$100. - Mismatched lengths.
XLOOKUPneeds the lookup range and result range to have the same number of rows (or columns).A2:A100withC2:C99returns an error. - Wrong sheet. After you copy a formula to another sheet, check that the sheet name in the range still points to the right place.
- Header offset. Starting a range at row 1 includes the header. This rarely causes
#N/Abut can return the header text for a key that matches it.
Fixing the data at its source
Wrapping every lookup in cleanup functions makes formulas long and slow. If the same dirty column feeds many lookups, clean it once in a helper column:
=ARRAYFORMULA(TRIM(CLEAN(SUBSTITUTE(A2:A100, CHAR(160), " "))))Put this in an empty column, then point lookups at the helper column. For text numbers, ARRAYFORMULA(VALUE(...)) does the same job for types.
The ranges above stop at row 100. If the data grows, use a range such as A2:A to include new rows. Open-ended ranges are safe in ARRAYFORMULA and lookups, but they include blank cells, so a blank search key can match an empty cell in the lookup column. Guard against that:
=IF(E2="", "", XLOOKUP(TRIM(E2), A2:A, C2:C, "Not found"))Separating "not found" from "broken"
Wrapping a lookup in IFERROR hides every error, including real mistakes such as a wrong column index. Prefer the narrower tools:
missing_valueinXLOOKUPreplaces only the not-found case.IFNAreplaces only#N/Aand leaves other errors visible.
=IFNA(VLOOKUP(TRIM(E2), A2:C100, 3, FALSE), "Not found")Use these only after you have confirmed the lookup is correct. A "Not found" message printed over a data problem looks like a valid result and hides the bug.
Quick reference
| Symptom | Likely cause | Check | Fix |
|---|---|---|---|
| Key looks present, lookup fails | Leading or trailing space | `LEN` of both cells | `TRIM` the key or the column |
| Still fails after `TRIM` | Non-breaking space | `UNICODE` of the last character | `SUBSTITUTE` code 160 with a space |
| Numeric ID fails | Number stored as text, or the reverse | `ISNUMBER` on both sides | `VALUE` or `&""` |
| Works in row 2, fails below | Missing `$` anchors | Click a lower cell and read its range | Anchor the range |
| Wrong value, no error | Approximate match on unsorted data | Last argument of `VLOOKUP` | Use `FALSE` |
| Every key fails | Key not in first column of range | Range start column | Move range or use `XLOOKUP` |
| Lookup of a missing key errors | Key truly absent | `COUNTIF` returns 0 | `missing_value` or `IFNA` |
Exercise
A formula =VLOOKUP(E2, A2:C100, 3, FALSE) returns #N/A for E2 = "P-102". The table in A2:A100 visibly contains "P-102". COUNTIF(A2:A100, E2) returns 1, but =E2=A5 (where A5 is the matching row) returns FALSE. LEN(E2) is 5 and LEN(A5) is 6. UNICODE(RIGHT(A5, 1)) returns 160. Explain the cause and write a lookup that works without changing the table.
Show answer
A5 ends with a non-breaking space (character code 160), which looks like a space but is not the ordinary space that TRIM removes. COUNTIF still counts the match because it compares more loosely, while the exact comparison and the lookup do not. Because VLOOKUP needs a real range, switch to XLOOKUP, which accepts an array expression for the lookup range: =XLOOKUP(E2, ARRAYFORMULA(TRIM(SUBSTITUTE(A2:A100, CHAR(160), " "))), C2:C100, "Not found"). A better long-term fix is a helper column that holds the cleaned values, with lookups pointed at it.
Exercise
An order sheet looks up customer IDs with =VLOOKUP(B2, Customers!$A$2:$C$500, 2, FALSE). IDs in Customers are numbers, but the IDs imported into column B are text, so every lookup returns #N/A. ISNUMBER(B2) returns FALSE. Give two fixes and say when you would pick each.
Show answer
Fix 1: convert the search key with =VLOOKUP(VALUE(TRIM(B2)), Customers!$A$2:$C$500, 2, FALSE). Pick this when the lookup table is the trusted source and you cannot or should not change the imported column. Fix 2: convert the imported column once, in a helper column with =ARRAYFORMULA(VALUE(TRIM(B2:B))), and look up from that column. Pick this when many formulas use the imported IDs, because it avoids repeating the conversion in every lookup.