Vlookup inconsistencies and remedy

Tigerexcel

Active Member
Joined
Mar 6, 2020
Messages
493
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
A 2 part question. Everyone has had the issue of Vlookup returning an error when a field is formatted as text and it requires a number. I run into this issue but it doesn't always return an error, sometimes even on the same files it returns inconsistent results. I notice it particularly when I download an external file. What is the main factor in causing this error, is it that the lookup value is formatted as text, or is the table array the major issue? The lookup formula will work sometimes when one or the other is formatted as General or Number but not always.
Even when you have formatted both the lookup value and the corresponding entry in the table array, a re-set is sometimes required to return the correct value. I usually do this by re-typing some of the lookup formula and that does the trick. I'd like to combine all these steps into a one-step process which will probably a macro so that I speed up the processing, anyone have such a macro?
 
Before uploading the file that Mark has asked for, it would be helpful to highlight a couple of cells where the vlookup result is incorrect and type the expected result in the cell next to it.
 
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Now I follow, the way you phrased your question earlier implied that the formula was returning the incorrect result, not that the formula was visible instead of a result.

After applying your desired formatting, running this will 'fix' any formulas

VBA Code:
Sub fixit()
    Cells.Replace What:="=", Replacement:="=", LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End Sub
 
Upvote 0
Thanks Jason but let's go back to my original question, why does it happen as it only happens on the odd occasion, and can the macro include changing the formatting so that it fixes the situation all in one go?
 
Upvote 0
What are the cells formatted as before you put in the formulas?
 
Upvote 0
That is your answer then, if they are formatted as Text then they remain as text.
To demonstrate format a blank cell as text and then type a number in the cell, you will see it will align to the left as it is text.
 
Upvote 0
That is your answer then, if they are formatted as Text then they remain as text.
To demonstrate format a blank cell as text and then type a number in the cell, you will see it will align to the left as it is text.
If I format the next column to General or Number and then copy the formula across it still returns the formula rather than the value, the only thing that works is if some part of the formula is re-typed.
 
Upvote 0
When you drag you create an exact copy, including any problems.
This will format the selected cells to general and 'fix' any formulas that appear as text.
VBA Code:
Sub fixit()
With Selection
    .NumberFormat = "General"
    .Replace What:="=", Replacement:="=", LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End Sub

Does that do what you need, or do you need to expand the format correction beyond that?

Applying 'General' to the whole sheet could remove the correct formatting from other cells if there are any that are formatted as date, currency, percentage, etc. If that is not going to be a problem for you then simply changing Selection to Cells should work.
 
Upvote 0
When you drag you create an exact copy, including any problems.
This will format the selected cells to general and 'fix' any formulas that appear as text.
VBA Code:
Sub fixit()
With Selection
    .NumberFormat = "General"
    .Replace What:="=", Replacement:="=", LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End Sub

Does that do what you need, or do you need to expand the format correction beyond that?

Applying 'General' to the whole sheet could remove the correct formatting from other cells if there are any that are formatted as date, currency, percentage, etc. If that is not going to be a problem for you then simply changing Selection to Cells should work.
Thanks that does the job.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,267
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top