# UDF Vlookup reference as text or as number

#### hendrik1985

##### New Member
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.

Best regards, Hendrik

### Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

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.

Replies
2
Views
271
Replies
7
Views
282
Replies
8
Views
705
Replies
33
Views
851
Replies
21
Views
791

1,196,385
Messages
6,014,980
Members
441,860
Latest member
Store154

### 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.

### Which adblocker are you using?

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

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