Why won't my VLOOKUP return a valid value ?

Hi All,

I have an Excel workbook of two sheets (Sheet A and Sheet B). I am using a value in Sheet A as the lookup value to return a value from Sheet B. Although the value exists in Sheet B, it returns a #N/A result.

I'm assuming this may be because there is a type mismatch between the lookup value (Text) and the lookup range (General).

How do I create a VBA macro to run through the Sheet A column to convert the cells from Text to General ?

Thanks in anticipation
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:

you can quickly turn numbers stored as text into real numbers like this: enter a 0 into a cell. Copy the cell. Select the cells with numbers stored as text. Use Paste Special, tick the Add option and hit OK. This will coerce the text to numbers. If the Vlookup then still returns an error, we need to dig deeper.

cheers, teylyn
TocogroupAuthor Commented:
That's really useful ! And it works a treat. What would the VBA code look like for this operation ?

Adjust column name (i.e "B:B" or "C:C", whatever is appropiate for you). Doesn't matter from what original format, it will always change to General.

    Selection.NumberFormat = "General"
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
@Kimputer, did you test that?

Tocogroup, just setting the number format with VBA will NOT change the text values to numbers.

Code along the following lines can be used:

Sub TextCoerceToNumbers()
' enter a zero into a cell and copy it
    Range("D1").FormulaR1C1 = "0"
' select the range with the numbers stored as text
' use paste special and the "add" option to coerce the text into numbers
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd, SkipBlanks:= _
        False, Transpose:=False
    Application.CutCopyMode = False
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rob HensonFinance AnalystCommented:
To automate there are a couple of routes, use an in-built routine or have one written.

In built - If you have the Error Notification function enabled, a green triangle will appear in the top left corner of the cell. One such example of this is numbers that are text, if you click on the error notification on the cell in question one of the options in the drop down menu that appears is to convert to numbers. If you have a number of them in a contiguous range, you can select the range and select the error menu on one and it will apply it to all.

VBA - Why bother when there is the in built function but you might want to include this in a data validation routine.

Selection.NumberFormat = "General"
For Each Cell in Selection
Cell.Value = Cell.Value
Next Cell

Changing the cell value will not have any impact if the cell format is text format, hence the need to change to General.

Rob H
TocogroupAuthor Commented:
Many thanks for that.
Just what I wanted.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.