Why VLOOKUP returns #N/A when the code is right there, and how to fix it
When Excel VLOOKUP returns #N/A for a code that is visibly in the lookup table, check the values one by one with LEN, ISTEXT, and UNICODE. Covers numbers stored as text, leading zeros, non-breaking spaces and zero-width spaces that TRIM cannot remove, ranges that shift without $, the omitted fourth argument, * and ? wildcards, and what to check before hiding errors with IFERROR.
You have a list of orders with product codes and want to pull the product name and unit price from a product master sheet. It is a routine job, and the formula is a single line: =VLOOKUP(A2,master!$A$2:$C$500,2,FALSE). Yet a few rows come back as #N/A. When you open the master sheet, the same code is right there, and comparing the two by eye shows no difference.
#N/A means “the value you searched for was not found”. It is not the error you get from a badly written formula. Excel compares values character by character, including their data type, so two values that look identical to a person do not match if their contents differ. The job at this point is not to rewrite the formula but to compare what is actually inside the lookup value and the master value.
This article first shows formulas that reveal the difference, then explains how to fix each cause. I checked the results by recalculating an .xlsx file in LibreOffice Calc 24.2. Where LibreOffice behaves differently from Excel, I say so, and statements about Excel are limited to what Microsoft’s documentation confirms.
The formulas use English function names and commas between arguments. If your regional settings use a comma as the decimal separator, Excel expects semicolons (;) between arguments instead.
Sample tables
The product master (sheet name master) has the following five rows. Only 1001 in column A is a number; the other codes are text. 00123 was entered as text to keep its leading zeros.
| A (code) | B (product) | C (unit price) | |
|---|---|---|---|
| 2 | 1001 | A5 notebook | 120 |
| 3 | A-200 | Gel ink pen, black | 150 |
| 4 | 00123 | Eraser | 80 |
| 5 | B-310 | Sticky notes 75 mm | 220 |
| 6 | C-020 | Small paper clips | 60 |
The table you search from contains codes exported as CSV from another system or copied from a web page. In the cells they look like 1001, A-200, and 123, but as the next section shows, their contents differ.
Find the difference with formulas first
Put the lookup value from a row that returns #N/A (here, A3) next to the master value that should match it (master!A3), and enter the following formulas in empty cells.
=A3=master!A3 → FALSE means Excel sees two different values
=LEN(A3) → Number of characters. More than you can see means hidden characters
=ISTEXT(A3) → TRUE for text, FALSE for a number or other type
=UNICODE(RIGHT(A3,1)) → Unicode code point of the last character
If =A3=master!A3 returns FALSE, the problem lies in the values, not in VLOOKUP. Then use LEN and ISTEXT to see whether the length or the type differs. UNICODE gives a clue when something is attached to the end: 32 is an ordinary space, 160 is a non-breaking space (NBSP), and 12288 is the full-width (ideographic) space used in Japanese and Chinese text. UNICODE, and UNICHAR used later, are available in Excel 2013 and later.
With the sample lookup values, the results were as follows.
| Lookup value (as displayed) | Actual content | LEN | ISTEXT | Last UNICODE | VLOOKUP (FALSE) |
|---|---|---|---|---|---|
A-200 |
Trailing ordinary space | 6 | TRUE | 32 | #N/A |
A-200 |
Trailing NBSP (U+00A0) | 6 | TRUE | 160 | #N/A |
A-200 |
Zero-width space (U+200B) in the middle | 6 | TRUE | 48 | #N/A |
123 |
The number 123 | 3 | FALSE | 51 | #N/A |
In the third row the last character is 0 (48), so UNICODE(RIGHT()) does not find it, but LEN is one more than the five characters you can see. A zero-width space has no width, so it is hard to notice even if you move the cursor through the cell.
To see every stray character at once, copy the cell value and paste it into the Invisible Character Checker. It shows the position and code of each NBSP or zero-width space. Processing happens in your browser, so pasting an internal code list does not send it anywhere.
Numbers mixed with text
In Excel, the number 1001 and the text "1001" are different values. If the master codes are numbers and the lookup values became text, for example during a CSV import, VLOOKUP does not match them. Microsoft’s article How to correct a #N/A error in the VLOOKUP function also lists mismatched data types as one of the causes. The telltale sign is a green triangle in the top-left corner of the cell with the message “Number Stored as Text”.
When I recalculated the same sheet in LibreOffice Calc, the text "1001" did find the number 1001. Excel behaves differently here, so a test in LibreOffice that shows no problem does not prove that Excel is fine.
The fix depends on which type you convert to.
=VLOOKUP(VALUE(A2),master!$A$2:$C$6,2,FALSE) Convert the text lookup value to a number
=VLOOKUP(--A2,master!$A$2:$C$6,2,FALSE) Same thing. -- turns text into a number
=VLOOKUP(A2&"",master!$A$2:$C$6,2,FALSE) Convert a numeric lookup value to text
Rather than converting in every formula, it is less trouble later to make the source column consistent. If the codes can safely be treated as numbers, select the column, open Data → Text to Columns, and click Finish without changing anything. Text digits in the column turn into numbers. You can also select the cells with green triangles and choose Convert to Number from the warning icon.
Codes with leading zeros
Converting a code with leading zeros to a number removes the zeros. If the master value 00123 is text and the lookup value 123 is a number, you cannot convert with VALUE. Instead, turn the lookup value into text with a fixed number of digits.
=VLOOKUP(TEXT(A5,"00000"),master!$A$2:$C$6,2,FALSE) → Eraser
Match "00000" to the length of the codes. If codes have different lengths, it is more reliable to re-import the lookup column as text from the start. Opening a CSV file by double-clicking lets Excel convert digits to numbers and drop the zeros. Instead, use Data → From Text/CSV, select the file, click Transform Data in the preview, change the data type of the code column to Text, and then load it.
TRIM removes trailing spaces, but not NBSP
If the value only has ordinary spaces before or after it, remove them with TRIM before the lookup.
=VLOOKUP(TRIM(A2),master!$A$2:$C$6,2,FALSE) → Gel ink pen, black
TRIM removes leading and trailing spaces and reduces runs of spaces between words to a single space. If the master codes may also contain spaces, rebuild the master column with TRIM as well.
Values copied from web pages or some business systems, however, may end with an NBSP, and TRIM does not remove it. According to Microsoft’s description of the TRIM function, TRIM removes the ASCII space (code 32) and does not by itself remove the Unicode character 160. CLEAN, which removes control characters, only covers codes 0 to 31, so it does not help with NBSP either. In my test, the sample value still returned #N/A after passing through TRIM or CLEAN.
Replace the NBSP with an ordinary space using SUBSTITUTE, then apply TRIM.
=VLOOKUP(TRIM(SUBSTITUTE(A3,UNICHAR(160)," ")),master!$A$2:$C$6,2,FALSE)
Microsoft’s Top ten ways to clean your data follows the same order: replace characters such as 160 with SUBSTITUTE, then use TRIM or CLEAN. Many guides write CHAR(160). That works on Windows set to a Western European code page, where 160 is NBSP, but CHAR returns a character from the computer’s character set, so on a Japanese, Chinese, or Korean system 160 is not necessarily NBSP. UNICHAR(160) specifies the Unicode code point and gives the same result everywhere.
Remove zero-width spaces and full-width spaces the same way.
=SUBSTITUTE(A4,UNICHAR(8203),"") Remove zero-width spaces (U+200B)
=SUBSTITUTE(A4,UNICHAR(12288),"") Remove full-width spaces (U+3000)
For a one-off check, absorbing the problem in the formula is fine. If the same source sends data every month, clean the lookup values in a helper column and pass that column to VLOOKUP. That makes it easier to trace the cause later.
#N/A starts appearing after you copy the formula down
A formula that works in the first row may produce more and more #N/A results after you copy it down. Without $, the lookup range shifts down by the same number of rows as the formula.
Row 1 =VLOOKUP(I1,master!A2:C6,2,FALSE)
Row 3 =VLOOKUP(I3,master!A4:C8,2,FALSE) ← A2 and A3 have dropped out of the range
In my test, the row 3 formula searching for A-200 had its range shifted to A4:C8, so A-200 in row 3 of the master was outside the range and the result was #N/A. Put the cursor on the range reference, press F4 to make it an absolute reference such as master!$A$2:$C$6, and then copy. If rows keep being added to the master, convert it to a table with Insert → Table and use the table name (for example Products) as the lookup range. The range then grows with the table and never needs fixing.
Did you omit the fourth argument?
If you omit VLOOKUP’s fourth argument (range_lookup), Excel treats it as TRUE (approximate match). Approximate match assumes the first column of the range is sorted in ascending order and finds the closest value that is less than or equal to the lookup value. When you need an exact match, as with product codes, always specify FALSE or 0.
With the argument omitted, you may not only get #N/A but also a value from the wrong row with no error at all. On the unsorted sample master, =VLOOKUP("A-200",master!$A$2:$C$6,2) returned “Eraser” instead of “Gel ink pen, black”. A silently wrong value is harder to catch than #N/A. Which row comes back depends on the sort order and on the spreadsheet application’s implementation, so do not rely on the result.
Approximate match is the right choice when the table lists the lower bounds of ranges, such as looking up a grade from a score or a fee bracket from an amount.
The lookup value contains * or ?
When the fourth argument is FALSE and you search for text, * in the lookup value is a wildcard for any string and ? for any single character. In my test, =VLOOKUP("A-2*",master!$A$2:$C$6,2,FALSE) returned the A-200 row.
This becomes a problem when codes or product names contain * or ? themselves. A search for the model number AB*1 can match different models such as AB-01 or ABC1. In a table listing AB-01, ABC1, and AB*1 in that order, searching for AB*1 returned the first row, AB-01. To search for *, ?, or ~ as literal characters, put ~ in front of them, as in AB~*1. If the lookup value is in a cell, escape it like this before passing it in. On the same table, this formula returned the AB*1 row.
=VLOOKUP(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,"~","~~"),"*","~*"),"?","~?"),master!$A$2:$C$6,2,FALSE)
VLOOKUP is also not case-sensitive. Searching for a-200 finds A-200. If a table uses upper- and lowercase to distinguish different codes, VLOOKUP alone cannot tell them apart.
The column you search is not the leftmost column of the range
VLOOKUP searches the leftmost column of the range and returns a value from a column counted to the right of it. If the column you search is to the right of the column you want to return, as when you want the code for a given product name, no change to the range will make it work. If the range accidentally starts at a column other than the product code, the code is not found and you get #N/A.
If you need to recount the column index from the left edge of the range for the third argument, see Find the third argument for VLOOKUP with an Excel column converter.
For tables where the search column is not on the left, Microsoft 365 and Excel 2021 or later offer XLOOKUP, which lets you specify the search column and the return column separately.
=XLOOKUP(A2,master!$B$2:$B$6,master!$A$2:$A$6,"Not found")
XLOOKUP uses exact match by default, and its fourth argument sets what to show when nothing is found. In files shared with older versions of Excel, INDEX and MATCH do the same job.
=INDEX(master!$A$2:$A$6,MATCH(A2,master!$B$2:$B$6,0))
Before you hide errors with IFERROR
A sheet full of #N/A looks messy, so it is tempting to blank them out with =IFERROR(VLOOKUP(...),""). But for every cause above, hiding the error makes it impossible to tell whether the code is really missing from the master or simply fails to match because of spaces or a type difference. If you blank errors without checking, a sales summary can miss some products without anyone noticing.
If you want to show that a value was not found, use IFNA instead of IFERROR. IFNA replaces only #N/A and leaves other errors visible, such as #REF! from a wrong column index.
=IFNA(VLOOKUP(TRIM(A2),master!$A$2:$C$6,2,FALSE),"Not found")
Filter the rows that show “Not found” and check whether each one is missing from the master or has a dirty lookup value before you continue. That way nothing quietly drops out of your totals.
Narrowing down the cause
When a code that should be there is not found, checking in this order usually leads to the cause.
- Is the fourth argument
FALSEor0? If it is omitted, you are using approximate match. - Does the lookup range have
$, and is its leftmost column the one you search? - If
=lookup_value=master_valuereturnsFALSE, compare the type withISTEXTand the length withLEN. - If the length is larger, identify the extra character with
UNICODE(RIGHT())or the Invisible Character Checker, and remove it withTRIMorSUBSTITUTE. - Does the lookup value contain
*,?, or~?
Dates that turn into numbers in Excel are another case of a value looking different from what it contains. If a lookup on dates fails to match, Why Excel dates turn into numbers like 46357 (Japanese only) may help.