UDF Vlookup reference as text or as number

hendrik1985

New Member
Joined
Jul 10, 2010
Messages
4
Hello experts,

In some worksheets where I use a vlookup function, it returns #N/A, because the format of the cells differs, some formatted as text, some as numbers, especially in query data.

With the following formula, I get a value, no matter the format.

=IF(ISTEXT(D2),VLOOKUP(VALUE(D2),$A$1:$B$5,2,0),VLOOKUP(TEXT(D2,"#"),$A$2:$B$5,2,0))

To work easy with this formula, I would like to make a user defined function (UDF) of this formula, but I dont know exactly how to do this.

Who can help me please?

Thanks in advance for your help.

Best regards, Hendrik
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,832
Office Version
  1. 2019
Platform
  1. Windows
I had the impression that your "lookup value" should match the "lookup range" - so, if there is text in A2:A5 then you should be looking for text, and if there is numbers in A2:A5 then you should be looking up numbers.

Wouldn't a simpler solution be:

Case 1, A2:A5 are numbers:
=VLOOKUP(VALUE(D2),A2:B5,2,0)

Case 2, A2:A5 are text:
=VLOOKUP(TEXT(D2,"@"),A2:B5,2,0)


For my part, I always coerce lookup values to numbers. If necessary, using a helper column, or running a quick macro to create true numbers in the column. The former is probably preferable on the reasoning that it will be easier for others to understand and use should your worksheet be passed on someday to another user. It's also very little trouble to use, has less performance hits, and requires no code modules for your worksheet to work properly.
 

Watch MrExcel Video

Forum statistics

Threads
1,130,169
Messages
5,640,548
Members
417,151
Latest member
ChickenTenderer

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
Top