ExcelyourTraining

Home › Blog

VLOOKUP not working? Check for hidden spaces before anything else

You've written a VLOOKUP, checked it twice, and it still says #N/A. You can see the product code sitting in the price list. You can see the same code on your order sheet. Excel insists they don't match.

Very often the reason is a space you can't see. In this post you'll learn how to prove that's the problem, how to fix it with TRIM, and what to do when TRIM itself doesn't seem to work.

The setup

Here's a short price list in columns A and B, and an order sheet where you look up each price.

A B
1 Code Price
2 KZN-101 R245
3 GP-202 R189
4 WC-303 R312

On the order sheet, cell E2 holds the code GP-202, and F2 has:

=VLOOKUP(E2,$A$2:$B$4,2,FALSE)

The result is #N/A, even though GP-202 is clearly in row 3.

Why VLOOKUP is not working when the value is right there

With FALSE as the last argument, VLOOKUP looks for an exact match. Exact means every character, including spaces. "GP-202" and "GP-202 " (with a space at the end) are different text as far as Excel is concerned.

Stray spaces sneak in all the time. Data copied from a website, exported from an accounting or payroll system, or typed by someone who hit the space bar before pressing Enter can all carry them. They're invisible in the cell, so you won't spot them by looking.

Step 1: prove it with a quick comparison

In an empty cell, type:

=E2=A3

If Excel returns FALSE when the two cells look identical, something hidden is different. Note that this comparison ignores upper and lower case, so a FALSE here isn't about capitals.

Step 2: count the characters with LEN

LEN tells you how many characters are in a cell, spaces included.

Formula Result
=LEN(A3) 6
=LEN(E2) 7

"GP-202" has six characters. If the order sheet's cell has seven, there's an extra character somewhere, and it's almost always a space at the start or end.

Step 3: fix it with TRIM inside the lookup

TRIM removes leading and trailing spaces, and squashes any run of spaces between words down to a single space. On its own:

=TRIM(E2)

returns "GP-202" with six characters. You don't need a helper column, though. You can put TRIM straight inside the VLOOKUP:

=VLOOKUP(TRIM(E2),$A$2:$B$4,2,FALSE)

Excel trims the code first, then looks it up. The result is R189.

This only cleans the value you're looking up. If the spaces are in the price list itself, in column A, trimming E2 won't help. In that case, clean the list: add a column next to it with =TRIM(A2), fill down, then copy that column and paste it back over the originals as values (Home > Paste > Paste Values). Delete the helper column once you're done.

When TRIM is not working: non-breaking spaces

Sometimes you wrap everything in TRIM and LEN still says 7. This catches people out, and it's one of the most common "Excel TRIM not working" complaints.

The likely cause is a non-breaking space. Web pages use them to stop words from splitting across lines, and they come along when you copy and paste from a browser or some web portals. It looks exactly like a normal space but it's a different character.

According to Microsoft's documentation, TRIM was designed to remove only the standard space character, code 32. The non-breaking space is code 160, so TRIM leaves it alone.

To check, look at the code of the last character:

=CODE(RIGHT(E2,1))

If the result is 160, you've found it. A normal space would show 32.

The fix is to swap every non-breaking space for a normal space with SUBSTITUTE, then let TRIM tidy up:

=TRIM(SUBSTITUTE(E2,CHAR(160)," "))

And inside the lookup:

=VLOOKUP(TRIM(SUBSTITUTE(E2,CHAR(160)," ")),$A$2:$B$4,2,FALSE)

It's a long formula, so if you need it on hundreds of rows, it's usually neater to clean the column once and then use a plain VLOOKUP.

What about CLEAN?

CLEAN is TRIM's partner. Where TRIM deals with spaces, CLEAN removes non-printing characters with codes 0 to 31. The one you'll meet most often is the line break (code 10), which turns up when an address or name was typed over two lines in a cell, or exported from another system.

You can combine them:

=TRIM(CLEAN(E2))

CLEAN doesn't remove code 160 either, so for web data you may still need the SUBSTITUTE step.

A quick worked example

Here's how the different fixes behave on a few messy codes from an order sheet.

What's in the cell LEN TRIM fixes it? Fix that works
"GP-202 " (normal space at end) 7 Yes TRIM(E2)
" WC-303" (two spaces in front) 8 Yes TRIM(E2)
"KZN-101" + non-breaking space 8 No TRIM(SUBSTITUTE(E2,CHAR(160)," "))
"GP-202" + line break 7 No TRIM(CLEAN(E2))

Other reasons for #N/A worth ruling out

Hidden spaces are a top cause, but not the only one. If LEN shows the right number of characters and the comparison still says FALSE, check these:

The habit to build

Before you trust any lookup on imported data, run LEN on a few lookup values and a few table values. If the counts don't match what you expect, clean first, look up second. It takes a minute and saves you from sending someone a quote with half the prices missing.

Try it yourself

Lesson 28, TRIM and CLEAN gives you a VLOOKUP that fails because of spaces, and you fix it by putting TRIM inside the lookup. If you want to get comfortable with LEN first, start with lesson 27. Both run in your browser and are graded as soon as you press Enter. Free, with no sign-up.