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?
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Connect With a Mentor 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

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 ?

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

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"
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.