VLOOKUP vs. INDEX/MATCH on variant array

jgscanlon

New Member
Joined
Jan 8, 2014
Messages
46
I have a multidimensional array with 'x' rows and 3 columns of data, and I'd like to perform a VLOOKUP on the array information stored in memory rather than calling the worksheet each time. I'm just not sure how to write the code to perform the VLOOKUP using INDEX/MATCH, and repeat that process for a variable number of items in a list.

The list of values is in column 1 in the array, and I'd like to return the corresponding value in column 3 from the array (table for data in the array is below).

I have a list of values from another sheet that I'll want to VLOOKUP or INDEX/MATCH against this array. If the value in my list doesn't exist in the array, then we just need a blank or NULL value.

Can anyone point me in the right direction?

Thank you!

External Portfolio Code

<tbody>
</tbody>
Base Currency Code

<tbody>
</tbody>
Master Portfolio Code

<tbody>
</tbody>
25D058

<tbody>
</tbody>
USD
00000960

<tbody>
</tbody>
25D080

<tbody>
</tbody>
USD
00001412

<tbody>
</tbody>
047761

<tbody>
</tbody>
USD
00001480

<tbody>
</tbody>
RUA0100

<tbody>
</tbody>
USD
00001894

<tbody>
</tbody>
RUL002

<tbody>
</tbody>
USD
00001896

<tbody>
</tbody>
097682

<tbody>
</tbody>
USD
00000026

<tbody>
</tbody>
247685

<tbody>
</tbody>
USD
00000021

<tbody>
</tbody>
003345

<tbody>
</tbody>
USD
00002441

<tbody>
</tbody>
970030

<tbody>
</tbody>
USD
00002623

<tbody>
</tbody>
146405

<tbody>
</tbody>
USD
00002685

<tbody>
</tbody>

<tbody>
</tbody>
 

Eric W

MrExcel MVP
Joined
Aug 18, 2015
Messages
8,811
You can just use VLOOKUP. Consider:

Code:
Sub test2()


    mytable = Range("A1:C11").Value
    
    code = "25D080"
    x = ""
    On Error Resume Next
    x = WorksheetFunction.VLookup(code, mytable, 3, 0)
    
    Debug.Print x
End Sub
There are several ways to handle the "not found" condition. Here I just set the result to what I want if it's not found, then use the On Error line to just continue if it hits "Not found" and the result variable isn't changed.
 

offthelip

Well-known Member
Joined
Dec 23, 2017
Messages
956
You haven't specified then names of either of the sheet which contain the lookuip data or the data to be lookup.
You haven't specified where you want the answer to go, so I assumed:
1: the table of lookup data was on sheet "lookupdata"
2: the data to be looked up was in column A of sheet "data"
3: the answers are written into column B of sheet Data
this is all done by using variant arrays which is a very fast way of doing vlookps in VBA
Code:
Sub test()
With Worksheets("LookupData")
' load all thje lookup table into a variant array
lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
Ldatar = Range(.Cells(1, 1), .Cells(lastrow, 3))
End With
With Worksheets("Data")
' load all the dat to be lookup in to a variant array
lastrw = .Cells(Rows.Count, "A").End(xlUp).Row
inarr = Range(.Cells(1, 1), .Cells(lastrw, 1))
' load and out put rangein a variant aarrayt
Range(.Cells(1, 2), .Cells(lastrw, 2)) ="" 
outarr = Range(.Cells(1, 2), .Cells(lastrw, 2))
For i = 1 To lastrw
 For j = 1 To lastrow
  If inarr(i, 1) = Ldatar(j, 1) Then
   outarr(i, 1) = Ldatar(j, 3)
   Exit For
  End If
 Next j
Next i


Range(.Cells(1, 2), .Cells(lastrw, 2)) = outarr
End With
End Sub
 
Last edited:

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
7,978
Office Version
2007
Platform
Windows
This is the option you request, make a match and get the 3 value.

Code:
Sub Test_Array()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim a() As Variant, b() As Variant, c() As Variant, n As Variant
  Set sh1 = Sheets("Sheet1")
  Set sh2 = Sheets("Sheet2")
  a = sh1.Range("A2:C" & sh1.Range("A" & Rows.Count).End(xlUp).Row).Value
  b = sh2.Range("A2:A" & sh2.Range("A" & Rows.Count).End(xlUp).Row).Value
  ReDim c(1 To UBound(b))
  For i = 1 To UBound(b)
    n = [COLOR=#0000ff]Application.Match[/COLOR](b(i, 1), [COLOR=#0000ff]Application.Index[/COLOR](a, 0, 1), 0)
    If IsError(n) Then
      c(i) = ""
    Else
      c(i) = a(n, 3)
    End If
  Next
End Sub
----------------------------------------

But the following, I think is faster. In the c () array you will have the results.

Code:
Sub Test_Array_2()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim a() As Variant, b() As Variant, c() As Variant, n As Variant
  Dim dict As Object
  Set sh1 = Sheets("Sheet1")
  Set sh2 = Sheets("Sheet2")
  a = sh1.Range("A2:C" & sh1.Range("A" & Rows.Count).End(xlUp).Row).Value
  b = sh2.Range("A2:A" & sh2.Range("A" & Rows.Count).End(xlUp).Row).Value
  ReDim c(1 To UBound(b))
  Set dict = CreateObject("Scripting.Dictionary")
  For i = 1 To UBound(a)
    dict.Add a(i, 1), a(i, 3)
  Next
  dict.CompareMode = TextCompare
  For i = 1 To UBound(b)
    If Not dict.Exists(b(i, 1)) Then
      c(i) = ""
    Else
      c(i) = dict(b(i, 1))
    End If
  Next
End Sub
 

Forum statistics

Threads
1,078,525
Messages
5,340,978
Members
399,402
Latest member
sri0197

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top