Macro for INDEX and MATCH

Remphan

New Member
Joined
Feb 5, 2013
Messages
31
Here is example file. Help pls with VBA code against formula. Because file may include more than 250K rows for sheet1 and the same for sheet2. So, formula works very slowly.

Also, very often the case that I need to compare the filtered bands and stretch the formula in the filtered range. Stretching the formula in the filtered band is very difficult, if he has a lot of rows. Therefore, if the table that we are looking for and from which the copy has filters - and it also needs to look only in the filtered range.

Often it is necessary to compare the way different ranges, would be very happy if the macro will be able to specify the range of the pop-up window. I would appreciate it if someone decides to help.

Cross
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi,

I'd like to but I cannot see your file. Perhaps if you posted the formula I could take a look.
 
Upvote 0
Hi,

I'd like to but I cannot see your file. Perhaps if you posted the formula I could take a look.

>>File (example)<<

1st sheet and table1 - may have a filter on columns. This is the table for which I needs to pull values from the second table.
2nd sheet - Originally located in another book, but for convenience I put it as a sheet in the first book with table.

Total, there are a couple of sheets in Workbook. For the first table, we need to pull out values from second table. The tables are different (only in example them are the same), but 1 an 1 table have completely or partially shared column in which a comparison is possible.

At present, a few problems:
1. Formula for a long time working on large data (250k+ rows)
2. The table of the first sheet may be set filter. And needs to compare visible values on the filter with the second sheet (which may also be a filter). It is necessary to compare both the filtered and unfiltered ranges only visible values);
3. 3. In large data uncomfortable stretching formula in the filtered range.

Sometimes I need only values after formula (copy/paste as unformatted values) and then remove sheet2, but sometimes it is required and automatic recalculation. That is sometimes the second table on the second sheet and leave to automatically recalculate.
 
Upvote 0
Sorry, but I do not have access to http://www.excelforum.com only MrExcel.com | Excel Resources | Excel Solutions.

I am not understanding the whole problem yet but I do have a VBA macro that will perform VLOOKUPs by using VBA Arrays and Dictionaries. It will look at key field in one column in a worksheet then copy on the corresponding columns from another worksheet. So it is like a VLOOKUP but it copies the data once so no recalculation is needed.

Code:
Sub VLookUp3()
    Dim arrT    As Variant      ' Table for Look-Up
    Dim arrI    As Variant      ' Input Column
    Dim arrO    As Variant      ' Output Array
    Dim ws1     As Worksheet    ' Data
    Dim ws2     As Worksheet    ' Look-Up Table
    Dim i       As Long
    Dim j       As Long
    Dim dic     As Dictionary
    Dim Key     As String
    
    Set dic = New Dictionary
   
    Set ws1 = ThisWorkbook.Worksheets("Sheet1")
    Set ws2 = ThisWorkbook.Worksheets("Sheet2")
    Set dic = CreateObject("Scripting.Dictionary")
        
    With ws2        ' Read in the Look Up Table
        arrT = .Range("A2:B" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        For i = 1 To UBound(arrT, 1): dic(arrT(i, 1)) = i: Next
    End With
    
    With ws1        ' Read in the Input Column
        arrI = .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
    End With
    
    ' Perform the Look Up
    ReDim arrO(1 To UBound(arrI, 1), 1 To UBound(arrT, 2) - 1)
    For i = 1 To UBound(arrO, 1)
        For j = 1 To UBound(arrO, 2)
            If dic.Exists(arrI(i, 1)) Then
                arrO(i, j) = arrT(dic(arrI(i, 1)), j + 1)
            Else
                arrO(i, j) = CVErr(xlErrNA)
            End If
        Next
    Next

    ' Write the results
    ws1.Range("D2").Resize(UBound(arrO, 1), UBound(arrO, 2)) = arrO

End Sub
The macro uses column A in Sheet1 as the value to be used for the look-up.
It matches that with column A in Sheet2 and copies in columns B and C from Sheet2 into column D of Sheet1.
 
Last edited:
Upvote 0
I think this is a better version of the code. The test data is at the end.
Code:
Sub VLookUp()
    Dim arrT    As Variant      ' Table for Look-Up
    Dim arrI    As Variant      ' Input Column
    Dim arrO    As Variant      ' Output Array
    Dim ws1     As Worksheet    ' Data
    Dim ws2     As Worksheet    ' Look-Up Table
    Dim i       As Long
    Dim j       As Long
    Dim dic     As Object
    Dim Key     As String
   
    Set ws1 = ThisWorkbook.Worksheets("Sheet1")
    Set ws2 = ThisWorkbook.Worksheets("Sheet2")
    Set dic = CreateObject("Scripting.Dictionary")
        
    With ws2        ' Read in the Look Up Table
        arrT = .Range("A2:D" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        For i = 1 To UBound(arrT, 1): dic(arrT(i, 1)) = i: Next
    End With
    
    With ws1        ' Read in the Input Column
        arrI = .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
    End With
    
    ' Perform the Look Up
    ReDim arrO(1 To UBound(arrI, 1), 1 To UBound(arrT, 2) - 1)
    For i = 1 To UBound(arrO, 1)
        For j = 1 To UBound(arrO, 2)
            If dic.Exists(arrI(i, 1)) Then
                arrO(i, j) = arrT(dic(arrI(i, 1)), j + 1)
            Else
                arrO(i, j) = CVErr(xlErrNA)
            End If
        Next
    Next

    ' Write the results
    ws1.Range("D2").Resize(UBound(arrO, 1), UBound(arrT, 2) - 1) = arrO

End Sub

Excel 2013
ABCDEF
1NLetterNameLkup#1Lkup#2Lkup#3
23CCharlieCCharliexx3
313MMikeMMikexx13
424XX-rayXX-rayxx24
51AAlphaAAlphaxx1
610JJulietJJulietxx10
72BBravoBBravoxx2
810JJulietJJulietxx10
92BBravoBBravoxx2
10555#N/A#N/A#N/A#N/A#N/A
1126ZZuluZZuluxx26
1223WWhiskeyWWhiskeyxx23
1323WWhiskeyWWhiskeyxx23
148HHotelHHotelxx8
1525YYankeeYYankeexx25
16444#N/A#N/A#N/A#N/A#N/A
179IIndiaIIndiaxx9
1825YYankeeYYankeexx25
1917QQuebecQQuebecxx17
2026ZZuluZZuluxx26
Sheet1
Cell Formulas
RangeFormula
B2=VLOOKUP($A2,Sheet2!$A$1:$C$27,2,FALSE)
C2=VLOOKUP($A2,Sheet2!$A$1:$C$27,3,FALSE)



Excel 2013
ABCD
1NumberLetterphonetic letterxx
21AAlphaxx1
32BBravoxx2
43CCharliexx3
54DDeltaxx4
65EEchoxx5
76FFoxtrotxx6
87GGolfxx7
98HHotelxx8
109IIndiaxx9
1110JJulietxx10
1211KKiloxx11
1312LLimaxx12
1413MMikexx13
1514NNovemberxx14
1615OOscarxx15
1716PPapaxx16
1817QQuebecxx17
1918RRomeoxx18
2019SSierraxx19
2120TTangoxx20
2221UUniformxx21
2322VVictorxx22
2423WWhiskeyxx23
2524XX-rayxx24
2625YYankeexx25
2726ZZuluxx26
Sheet2
 
Upvote 0
I think this is a better version of the code. The test data is at the end.

This is almost exactly what I need (macro). Only a small request, if there is little opportunity to expand its functionality.

In my case:

for single column C

Code:
C2=IFERROR(INDEX(Sheet2!$B$2:$B$27,MATCH(A2,Sheet2!$A$2:$A$27,0)),"-")

and for a range (can stretch formula till F and next columns)

Code:
D2=IFERROR(INDEX(Sheet2!B$2:B$27,MATCH(A2,Sheet2!$A2:$A$27,0)),"-")

Several problems. Table 1 on the sheet may have a filter on the sheet and the table 2 may also have a filter. It is necessary to compare only the visible (filtered) values and paste only visible columns (what I need to pull up from sheet2 to sheet1). If the filter is not - then compare all visible.

The second trouble is that often need to compare many different tables, then there will need to be changed every time the macro code. It would be cool if macro could show the request that compare with what (universal for any tables) And how we paste (like C2 or D2 example formula way).

It is a pity that it's difficult yet for me to write such things, can only complicated formulas, macros little harder. So If you're a little extend the functionality of the macro, it will be very cool).
 
Upvote 0
I am hesitating because as a programmer I can see that it is quite general already. Another problem is that what you may be asking for will be several times the effort for me and another is that I still don't know exactly how you intend to use it. Use of "filters" should not affect the look-up. They are applied afterwards?

For example:
Code:
C2=IFERROR(INDEX(Sheet2!$B$2:$B$27,MATCH(A2,Sheet2!$A$2:$A$27,0)),"-")
becomes:
Rich (BB code):
Sub VLookUp()
    Dim arrT    As Variant      ' Table for Look-Up
    Dim arrI    As Variant      ' Input Column
    Dim arrO    As Variant      ' Output Array
    Dim ws1     As Worksheet    ' Data
    Dim ws2     As Worksheet    ' Look-Up Table
    Dim i       As Long
    Dim j       As Long
    Dim dic     As Object
    Dim Key     As String
   
    Set ws1 = ThisWorkbook.Worksheets("Sheet1")
    Set ws2 = ThisWorkbook.Worksheets("Sheet2")
    Set dic = CreateObject("Scripting.Dictionary")
        
    With ws2        ' Read in the Look Up Table
        arrT = .Range("A2:B" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        For i = 1 To UBound(arrT, 1): dic(arrT(i, 1)) = i: Next
    End With
    
    With ws1        ' Read in the Input Column
        arrI = .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
    End With
    
    ' Perform the Look Up
    ReDim arrO(1 To UBound(arrI, 1), 1 To UBound(arrT, 2) - 1)
    For i = 1 To UBound(arrO, 1)
        For j = 1 To UBound(arrO, 2)
            If dic.Exists(arrI(i, 1)) Then
                arrO(i, j) = arrT(dic(arrI(i, 1)), j + 1)
            Else
                arrO(i, j) = "-"
            End If
        Next
    Next

    ' Write the results
    ws1.Range("C2").Resize(UBound(arrO, 1), UBound(arrO, 2)) = arrO

End Sub
I could make the red parts variables which you could define altogether at the start of the code;
Or I could make the macro callable from another macro that would supply the same data;
Or It could read the data from a worksheet.

If you need to have several different copies and you want to know which one is which then you can add comments.
 
Upvote 0

Forum statistics

Threads
1,215,767
Messages
6,126,777
Members
449,336
Latest member
p17tootie

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