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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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.
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,557
Latest member
richa mishra

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