INDEX MATCH multiple values in same row

surkdidat

Well-known Member
Joined
Oct 1, 2011
Messages
582
Office Version
  1. 365
I need to match A2 and D2 (in a sheet called "5.12") - against Sheet1, (A2 against Column B, and D against Column F)

This will go down for several hundred lines.

I appreciate the easy solution would be to add a column combing the two and then matching that, but the sheet directly gets pulled into an Access database so I cannot fiddle with the format

So in effect (in real terms) I will have a Category Number (for example 12345) which will have 20 or so lines of data in the Master Sheet, one for each specific, (Colour, Weight, Size etc)

If I use Index Match one one value it obviously only picks up the first match. I have tried to do INDEX MATCH MATCH on 2 columns, on 2 values in the same row, but it does not seem to like it.

Any help appreciated!

(For reference I have tried
Excel Formula:
=INDEX(RawData!I:I,MATCH(A2,RawData!B:B,0),MATCH(D2,RawData!F:F,0))

SHEET 5.12Column A - BatchColumn B - Cat_NumberColumn D - Cat_DetailColumn E (Result of INDEX MATCH)
Row 1100012345ColourBlue
Row 2100012345Weight1.5
Row 3100012345ShapeSquare
Row 4100023456ColourWhite
Row 5100023456Weight2.0
Row 6100023456ShapeRound


RawData Column AColumn B - Category NoColumn F - Cat DetailsColumn I - (Data that needs to be pulled from Index Match)
Row 112345ColourBlue
Row 212345ShapeSquare
Row 334564ColourPurple
Row 412345Weight1.5
Row 523456ColourWhite
Row 623456Weight2.0
Row 734564Weight4.0
Row 823456ShapeRound
Row 934564ShapeTriangle
 
The main data may not be populated in order, so would need the macro to run to the last row every time, and not stop on the first row/cell that is empty.

Are there blank rows in RawData sheet and/or the destination sheets?

M.
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
How about
VBA Code:
Sub surkdidat()
   Dim Ary As Variant
   Dim Dic As Object
   Dim r As Long, c As Long
   
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("Sheet1")
      Ary = .Range("B2:M" & .Range("B" & Rows.Count).End(xlUp).Row)
   End With
   For r = 1 To UBound(Ary)
      If Ary(r, 1) <> "" Then
         Dic(Ary(r, 1) & "|" & Ary(r, 5)) = Application.Index(Ary, r, Array(7, 8, 9, 10, 11))
      End If
   Next r
   With Sheets("Sheet2")
      Ary = .Range("A2:K" & .Range("A" & Rows.Count).End(xlUp).Row)
   End With
   For r = 1 To UBound(Ary)
      If Dic.Exists(Ary(r, 1) & "|" & Ary(r, 4)) Then
         For c = 1 To 5
            Ary(r, c + 6) = Dic(Ary(r, 1) & "|" & Ary(r, 4))(c)
         Next c
      End If
   Next r
   Sheets("Sheet2").Range("A2").Resize(UBound(Ary), 11).Value = Ary
End Sub
Change sheet names to suit.
 
Upvote 0
Just to test performance with only one target sheet - sheet 5.12 in this case (not sure about performance with more than 400k rows - not tested with as many rows)

Assuming...

RawData
08122021 Testes.xlsm
BCDEFGHIJKLM
1Category NoCat DetailsData_IData_JData_KData_LData_M
212345ColourBluejjj1kkk1lll1mmm1
312345ShapeSquarejjj2kkk2lll2mmm2
434564ColourPurplejjj3kkk3lll3mmm3
5
612345Weight1.5jjj4kkk4lll4mmm4
723456ColourWhitejjj5kkk5lll5mmm5
8
9
1023456Weight2.0jjj6kkk6lll6mmm6
1134564Weight4.0jjj7kkk7lll7mmm7
1223456ShapeRoundjjj8kkk8lll8mmm8
1334564ShapeTrianglejjj9kkk9lll9mmm9
14
RawData


Sheet 5.12 results in columns G:K
08122021 Testes.xlsm
ABCDEFGHIJK
1BatchCat_NumberCat_DetailCol_ICol_JCOL_KCol_LCol_M
2100012345ColourBluejjj1kkk1lll1mmm1
3
4100012345Weight1.5jjj4kkk4lll4mmm4
5100012345ShapeSquarejjj2kkk2lll2mmm2
6
7
8100023456ColourWhitejjj5kkk5lll5mmm5
9100023456Weight2.0jjj6kkk6lll6mmm6
10100023456ShapeRoundjjj8kkk8lll8mmm8
5.12


Macro
VBA Code:
Sub aTestV2()
    Dim dic As Object
    Dim lr As Long, vData As Variant, i As Long
    Dim vResult As Variant
   
    Set dic = CreateObject("Scripting.Dictionary")
    dic.CompareMode = vbTextCompare
   
    'Activate RawData
    Sheets("RawData").Activate
    lr = Cells(Rows.Count, "B").End(xlUp).Row
    vData = Range("B2:M" & lr).Value
 
    'Store data in the dictionary
    For i = 1 To UBound(vData, 1)
        If vData(i, 1) <> "" Then
            dic(vData(i, 1) & "|" & vData(i, 5)) = _
                Array(vData(i, 8), vData(i, 9), vData(i, 10), vData(i, 11), vData(i, 12))
        End If
    Next i
   
    'Activate 5.12
    Sheets("5.12").Activate
    lr = Cells(Rows.Count, "B").End(xlUp).Row
    vData = Range("B2:D" & lr).Value
    vResult = Range("G2:K" & lr)
   
    'Get the results from the dictionary
    For i = 1 To UBound(vData, 1)
        If vData(i, 1) <> "" Then
            vResult(i, 1) = dic(vData(i, 1) & "|" & vData(i, 3))(0)
            vResult(i, 2) = dic(vData(i, 1) & "|" & vData(i, 3))(1)
            vResult(i, 3) = dic(vData(i, 1) & "|" & vData(i, 3))(2)
            vResult(i, 4) = dic(vData(i, 1) & "|" & vData(i, 3))(3)
            vResult(i, 5) = dic(vData(i, 1) & "|" & vData(i, 3))(4)
        End If
    Next i
    'Tranfer to columns G:K
    With Range("G2:K" & lr)
        .NumberFormat = "@"
        .Value = vResult
    End With
End Sub

Hope this helps

M.
 
Upvote 0
I would advise against trying my code, if it's not too late, I don't think it likes the use of Index, as it's taking way too long. :(
 
Upvote 0
This is a lot faster than my original code.
VBA Code:
Sub surkdidat()
   Dim Ary As Variant
   Dim Dic As Object
   Dim r As Long, c As Long
 
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("Sheet1")
      Ary = .Range("B2:M" & .Range("B" & Rows.Count).End(xlUp).Row)
   End With
   For r = 1 To UBound(Ary)
      If Ary(r, 1) <> "" Then
         Dic(Ary(r, 1) & "|" & Ary(r, 5)) = Array(Ary(r, 7), Ary(r, 8), Ary(r, 9), Ary(r, 10), Ary(r, 11))
      End If
   Next r
   With Sheets("Sheet2")
      Ary = .Range("A2:K" & .Range("A" & Rows.Count).End(xlUp).Row)
   End With
   For r = 1 To UBound(Ary)
      If Dic.Exists(Ary(r, 1) & "|" & Ary(r, 4)) Then
         For c = 0 To 4
            Ary(r, c + 7) = Dic(Ary(r, 1) & "|" & Ary(r, 4))(c)
         Next c
      End If
   Next r
   Sheets("Sheet2").Range("A2").Resize(UBound(Ary), 11).Value = Ary
End Sub

Both the above code & Marcelo's take about 1.7secs with 300,000 rows & 9000 rows
 
Upvote 0
Fluff
Your code uses Ary(r,7) to Ary(r,11)
Mine, vData(i,8) to vData(i,12) :unsure:

Are your data in different columns?

M.
 
Upvote 0
Are your data in different columns?
Nope, I just can't count. :cry:
With that correction the line should be
VBA Code:
         dic(Ary(r, 1) & "|" & Ary(r, 5)) = Array(Ary(r, 8), Ary(r, 9), Ary(r, 10), Ary(r, 11), Ary(r, 12))
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,395
Members
449,081
Latest member
JAMES KECULAH

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