Is there a VBA equivalent of XLookup for arrays?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,532
Office Version
  1. 365
Platform
  1. Windows
Suppose I have a VBA array (n,2) something like this, but in a VBA variable, not a sheet range.
1674095965510.png

Is there something in VBA like XLookup in Excel that will do this, but with a VBA array?
Weighted Ratings.xlsm
PQRS
5A9C4
6B7E1
7C4A9
8D2
9E1
Test
Cell Formulas
RangeFormula
S5:S7S5=XLOOKUP(R5,$P$5:$P$9,$Q$5:$Q$9)


I know I can do it in a loop. Since the table is small, that's probably OK. I just wondered if there is something better. Thanks
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Im not currently at my computer, so haven't tested this, I would've thought you could just use `XLOOKUP` from VBA with `WorksheetFunction`. So:

VBA Code:
Result = WorksheetFunction.Xlookup(....

And then add the usual parameters like you would when using it in the worksheet (I don't remember what they are off the top of my head).
 
Upvote 0
This format will work.

VBA Code:
Sub test_xlookup()

    Dim rng As Range
    Dim arr As Variant
    
    Set rng = Range("A1").CurrentRegion
    arr = rng.Value
    
    Dim vresult As Variant
    
    With WorksheetFunction
        vresult = .XLookup("x", .Index(arr, 0, 1), .Index(arr, 0, 2), "Not Found")
    End With
    
    Debug.Print vresult


End Sub
 
Upvote 0
Solution
Im not currently at my computer, so haven't tested this, I would've thought you could just use `XLOOKUP` from VBA with `WorksheetFunction`. So:

VBA Code:
Result = WorksheetFunction.Xlookup(....

And then add the usual parameters like you would when using it in the worksheet (I don't remember what they are off the top of my head).
Ok. Suppose this array is named arrTest.

1674102135732.png

The XLookup syntax is
VBA Code:
=XLOOKUP(lookup_value, lookup_array, return_array, [if_not_found], [match_mode], [search_mode])
How do I specify the 2nd and 3rd parameters? For lookup_array I want column 1. For return_array I want column 2.
 
Upvote 0
This format will work.

With WorksheetFunction
vresult = .XLookup("x", .Index(arr, 0, 1), .Index(arr, 0, 2), "Not Found")
End With

End Sub
[/CODE]
It was the index function that I didn't think of. Thanks. That works perfectly.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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