UDF - xlookup

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
20,615
Office Version
  1. 365
Platform
  1. Windows
I wrote the following to do a cross reference kind of lookup. The idea behind this is to find a value based on the intersection of two values. So in this example, it would find the value where D13 and 25 intersect. It seems to work, if you find it useful or there is a better way to do this, any comments are appreciated.

The syntax for the function is =xlookup(range,string or cell reference to look for vertically, string or cell reference to look up horizontally)

Code:
Public Function xlookup(xrange As Range, vsearch As String, hsearch As String)
Dim a As Range, vrange As Range, hrange As Range, c As Range, d As Range
Dim f As Integer, g As Long, i As Long, j As Integer
Dim FirstAddress As String, SecondAddress As String

Set a = xrange

f = a.Column
g = a.Row
i = Cells(65536, f).End(xlUp).Row
j = Cells(g, 256).End(xlToLeft).Column

Set vrange = Range(Cells(g, f), Cells(i, f))
Set hrange = Range(Cells(g, f), Cells(g, j))

With vrange
    Set c = .Find(vsearch)
    If Not c Is Nothing Then FirstAddress = c.Address
End With

With hrange
    Set d = .Find(hsearch)
    If Not d Is Nothing Then SecondAddress = d.Address
End With

If Not c Is Nothing And Not d Is Nothing Then
    xlookup = Cells(c.Row, d.Column)
Else
    xlookup = "N/A"
End If

End Function
xlookup.xls
ABCDEFGHIJ
142
2
3
4
5
6139142225
7A123456
8B789101112
9C131415161718
10D192021222324
11E252627282930
12F313233343536
13G373839404142
14H434445464748
15I495051525354
16J555657585960
17K616263646566
18L676869707172
19M737475767778
Sheet1
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi,

I would prefere a normal INDEX( MATCH() MATCH()) formula. Then you do not need the code (getting macro alerts) and you can specify match criteria ( exact match or...)
 
Upvote 0
Curious, and maybe I'm missing something obvious, but is there an advantage to this beyond the native INDEX MATCH MATCH functions?


Edit, I see fairwinds' similar inquiry also.
 
Upvote 0
im trying to use xlookup in excel sheet,however it does not give me any result..it simply shows me the formula that i wrote in that column only..nothing else...is there is something wrong..??

Saurabh
 
Upvote 0
OK, I'm trying to do it that way but obviously something is amiss here, because I'm not getting the right result? Xlookup was an idea I had, syntax is a lot simpler, but maybe it isn't the way to go. What am I doing wrong with the INDEX MATCH MATCH?
xlookup.xls
ABCDEFGHIJ
135
242
3
4
5
6139142225
7A123456
8B789101112
9C131415161718
10D192021222324
11E252627282930
12F313233343536
13G373839404142
14H434445464748
15I495051525354
16J555657585960
17K616263646566
18L676869707172
19M737475767778
Sheet1
 
Upvote 0
Never mind, figured it out:
=INDEX(E7:J19,MATCH("G",D7:D19,0),MATCH(25,E6:J6,0))
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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