Excel VBA - using lookup functions on a two-dimensional array

Harry Flashman

Active Member
Joined
May 1, 2011
Messages
361
I am trying to figure out how to use Excel lookup functions (such as VLOOKUP, HLOOKUP, INDEX, and MATCH) to return the data according to certain criteria from a two-dimensional array.

Let's say I have an two dimensional array like the following:

Code:
Sub LookupArray()
Dim MyArray(0 To 1, 0 To 3)

MyArray(0, 0) = 1
MyArray(0, 1) = 2
MyArray(0, 2) = 3
MyArray(0, 3) = 4

MyArray(1, 0) = "A"
MyArray(1, 1) = "B"
MyArray(1, 2) = "C"
MyArray(1, 3) = "D"

End Sub

And let's say I want to return the Letter based on a numeric value in my array.

Code:
Dim MyStr As String
MyStr = WorksheetFunction.HLookup(3, MyArray, 2, 0)

The above code returns C.

But what if my criteria value is B, for example. How can I get it to return the corresponding number?

Likewise, let's say I want to return the position of a numeric value the above array I can use the MATCH and INDEX functions.

Code:
Dim i As Integer
i = WorksheetFunction.Match(3, WorksheetFunction.Index(MyArray, 1, 0), 0)

The above code returns 3.

But how can I do the same thing with a letter? The various permutations I have tried keep returning error messages.

Any help would be greatly appreciated. Cheers.
 
Last edited:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
This happens to me every time. I spend ages messing around getting nowhere. Then I post to a forum, and then minutes later I figure it out.

This will return the position of the letter within row 2
Code:
MyStr = WorksheetFunction.Match("C", WorksheetFunction.Index(MyArray, 2, 0), 0)
Debug.Print MyStr ' returns 3

This will return the corresponding numeric value (from row 1) based on a known letter (from row 2).
Code:
MyStr = Application.Index(MyArray, 1, WorksheetFunction.Match("D", WorksheetFunction.Index(MyArray, 2, 0), 0))
Debug.Print MyStr ' returns 4
End Sub
 
Upvote 0
I suppose this bring me to my next question. Is it better to find a value within an array using an Excel function or is it better to loop through the array and find the value that way?
 
Upvote 0

Forum statistics

Threads
1,216,524
Messages
6,131,176
Members
449,629
Latest member
Mjereza

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