# VLOOKUP Hack #8: Extra Spaces

Here’s a VLOOKUP question for you: What happens when the lookup value includes extra spaces? For example, a few trailing spaces, or leading spaces, or, extra spaces in the middle of the text string. When the lookup value has extra spaces, but the lookup range values don’t, VLOOKUP runs into problems. This leads us to our next hack.

I’ve prepared a video and full write-up for reference.

## Narrative

Let’s try to visualize the issue before we get to the hack. We have a report structure, as shown below.

We need to retrieve the related Cost from Table1, shown below.

So, we write the following formula into D7:

`=VLOOKUP(B7, Table1, 2, 0)`

And, to our surprise, it returns an error, as shown below.

What’s going on? Is Excel broken? Did I write the formula incorrectly? The formula looks perfect … so why the error? When we inspect the data closely, we see that the lookup values have extra spaces. Specifically, a bunch of trailing spaces.

So, instead of looking up “AB101” Excel is trying to find “AB101   ” (w/trailing spaces). Our 4th argument is 0, and since it can’t find an exact match, it returns #N/A.

So, what are we supposed to do? Go in there and delete all of the trailing spaces by hand? No, that would take too long. This leads to the hack.

## Hack

Rather than remove the extra spaces by hand, we’ll get an assist from another function, TRIM.

Hack: Use TRIM in the 1st argument

TRIM as you may have guessed removes excess spaces. Namely, leading, trailing, and extra spaces between words.

So, we just modify our formula above by using TRIM in the first argument. Something like this:

`=VLOOKUP(TRIM(B7), Table1, 2, 0)`

We hit enter, and bam … it works! We push the formula down, and the results are shown below.

The sample file includes the formulas in case you’d like to check it out.

If you have any other fun VLOOKUP Hacks, please share by posting a comment below!

Get a quick email notice when a new Excel article is available

• This field is for validation purposes and should be left unchanged.

1. Gemma

Hi Jeff
I had never come across Trim before, so thank you for this. Normally I would use :
=VLOOKUP((B7 & “*”), Table1, 2, 0)

1. Jeff Lenning Post author

Hi Gemma … wildcards work awesome with VLOOKUP, and we’ll talk more about them in our next hack 🙂

Thanks
Jeff

2. Geof

What if the extra spaces are in Table1? Can we use TRIM in the 2nd argument of VLOOKUP, or do we have to trim the table in advance of the lookup?

1. Jeff Lenning Post author

Hi Geof … we can use wildcards, and we cover that in the next hack 🙂

Thanks
Jeff

3. Moe

How did you know where the spaces were?

Also how can I just get rid of all spaces even between 2 texts?

I always just use the trim function then copy and paste values then do the vlookup. Never tried it nested. Looking forward.

Thanks

1. Jeff Lenning Post author

Hi Moe!
I had to investigate the data to find the trailing spaces, and jim posted a comment here that shows how to use the SUBSTITUTE function to remove all spaces, not just extra spaces like TRIM 🙂
Thanks,
Jeff

4. jim

I often have to lookup vehicle reg nos, which are sometimes entered with spaces but not necessarily in predictable positions
My trick is to use SUBSTITUTE to “clean” the entry:
MATCH(SUBSTITUTE(regnoref,” “,””),lookuprange,)
oh and yeah, I’ve finally got in the habit of INDEX-MATCHing, unless it’s trivial

1. Jeff Lenning Post author

Nice!! Thanks for sharing this great idea 🙂
Thanks
Jeff

5. Leezip

Thank you for the information

6. Diedre Love