Index match statement with multiple criteria in VBA

sjk1193

New Member
Joined
Nov 12, 2018
Messages
29
So what I am trying to do is if data in the DATA tab in columns 1 & 5 match with columns 1 & 2 in the INDEX tab then take the data in the 3 adjacent columns and place it in the UPLOAD tab. After this is done I need to go to the next row and do the same

Example.

if Robert def in the DATA tab matches def Robert in the INDEX tab then I need to take FFFF, H, 8527 and place it in the UPLOAD tab

I basically need to do this for all data in the DATA tab



INDEX
[TABLE="width: 500"]
<tbody>[TR]
[TD]1[/TD]
[TD]2[/TD]
[TD]3[/TD]
[TD]4[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]abc[/TD]
[TD]Augusta[/TD]
[TD]AAAA[/TD]
[TD]H[/TD]
[TD]123[/TD]
[/TR]
[TR]
[TD]abc[/TD]
[TD]Palmaire[/TD]
[TD]BBBB[/TD]
[TD]H[/TD]
[TD]65464[/TD]
[/TR]
[TR]
[TD]abc[/TD]
[TD]Tamaz[/TD]
[TD]CCCC[/TD]
[TD]H[/TD]
[TD]4545[/TD]
[/TR]
[TR]
[TD]abc[/TD]
[TD]ZCR[/TD]
[TD]DDDD[/TD]
[TD]H[/TD]
[TD]453[/TD]
[/TR]
[TR]
[TD]abc[/TD]
[TD]Moir[/TD]
[TD]EEEE[/TD]
[TD]C[/TD]
[TD]453453[/TD]
[/TR]
[TR]
[TD]def[/TD]
[TD]Robert[/TD]
[TD]FFFF[/TD]
[TD]H[/TD]
[TD]8527[/TD]
[/TR]
[TR]
[TD]def[/TD]
[TD]JCA[/TD]
[TD]GGG[/TD]
[TD]C[/TD]
[TD]786[/TD]
[/TR]
[TR]
[TD]def[/TD]
[TD]Caren[/TD]
[TD]SDEFSDF[/TD]
[TD]C[/TD]
[TD]000[/TD]
[/TR]
[TR]
[TD]def[/TD]
[TD]Nelson[/TD]
[TD]SDFVB[/TD]
[TD]C[/TD]
[TD]111[/TD]
[/TR]
[TR]
[TD]def[/TD]
[TD]Levy[/TD]
[TD]DFD[/TD]
[TD]C[/TD]
[TD]222[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D][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
How about
Code:
Sub Getdata()
   Dim Cl As Range
   Dim Dws As Worksheet, Iws As Worksheet
   Dim Vu As String
   Dim Itm As Variant
   Dim i As Long
   
   Set Dws = Sheets("Data")
   Set Iws = Sheets("Index")
   With CreateObject("scripting.dictionary")
      For Each Cl In Dws.Range("A2", Dws.Range("A" & Rows.Count).End(xlUp))
         .Item(Cl.Offset(, 4).Value & "|" & Cl.Value) = Empty
      Next Cl
      For Each Cl In Iws.Range("A2", Iws.Range("A" & Rows.Count).End(xlUp))
         Vu = Cl.Value & "|" & Cl.Offset(, 1).Value
         If .Exists(Vu) Then .Item(Vu) = Array(Cl.Offset(, 2).Value, Cl.Offset(, 3).Value, Cl.Offset(, 4).Value)
      Next Cl
      For Each Itm In .Items
         If IsArray(Itm) Then
            i = i + 1
            Sheets("Input").Range("C" & i).Resize(, 3).Value = Itm
         End If
      Next Itm
   End With
End Sub
 
Upvote 0
Thank you!!
Would you mind commenting on what each section does though because I'm pretty sure I follow when I enter it into VBA but I just want to make sure so that I am not misleading myself
 
Upvote 0
Best idea is for you comment the code & post it back here, as I have no idea what your knowledge level is & therefore how much detail to go into.
That way I can correct any misunderstandings & comment anything that you don't understand.
 
Upvote 0
I'm just starting to learn VBA so as much detail as possible would help. I normally F8 through the lines of code to see if i can follow whats going on and then google stuff I'm not sure about
 
Last edited:
Upvote 0
As I said, it's best if you comment it as best you can & post back
 
Upvote 0
Code:
Dim Cl As Range
   Dim DATA As Worksheet, CIndex As Worksheet, FIndex As Worksheet
   Dim Vu As String
   Dim Itm As Variant
   Dim i As Long
   
   Set DATA = Sheets("DATA")
   Set CIndex = Sheets("Client Index")
   Set FIndex = Sheets("Fund Index")
   
   With CreateObject("scripting.dictionary")
      'looks end range in DATA tab
      For Each Cl In DATA.Range("A2", DATA.Range("A" & Rows.Count).End(xlUp))
        'scrolls through columns in DATA tab ascending order
         .Item(Cl.Offset(, 4).Value & "|" & Cl.Value) = Empty
      Next Cl
      'looks end range in Client Index  tab
      For Each Cl In CIndex.Range("A2", CIndex.Range("A" & Rows.Count).End(xlUp))
         Vu = Cl.Value & "|" & Cl.Offset(, 1).Value
         If .Exists(Vu) Then .Item(Vu) = Array(Cl.Offset(, 2).Value, Cl.Offset(, 3).Value, Cl.Offset(, 4).Value)
      Next Cl
      'Pasts all the matching data in Upload tab
      For Each Itm In .Items
         If IsArray(Itm) Then
            i = i + 1
            Sheets("Upload").Range("C" & i).Offset(1, 0).Resize(, 3).Value = Itm
         End If
      Next Itm
   End With


I'm just not sure how it searches and matches or what the "|" is
 
Last edited:
Upvote 0
Ok, added more comments
Code:
   Dim Cl As Range
   Dim DATA As Worksheet, CIndex As Worksheet, FIndex As Worksheet
   Dim Vu As String
   Dim Itm As Variant
   Dim i As Long
   
   Set DATA = Sheets("DATA")
   Set CIndex = Sheets("Client Index")
   Set FIndex = Sheets("Fund Index")
   
   'Creates a Dictionary
   With CreateObject("scripting.dictionary")
      'looks end range in DATA tab
      For Each Cl In DATA.Range("A2", DATA.Range("A" & Rows.Count).End(xlUp))
        'scrolls through columns in DATA tab ascending order
         'Concatenates the 2 cell values with a | in between & adds the resultant value to the dictionary (if it doesn't already exist) with nothing in the item
         .Item(Cl.Offset(, 4).Value & "|" & Cl.Value) = Empty
      Next Cl
      'looks end range in Client Index  tab
      For Each Cl In CIndex.Range("A2", CIndex.Range("A" & Rows.Count).End(xlUp))
         'Concatenates the 2 cell values with a | in between & puts it in the Vu variable
         Vu = Cl.Value & "|" & Cl.Offset(, 1).Value
         ' if the Vu value alreadu exists in the dictionary it adds an array in the item containing the 3 cell values you want returned
         If .Exists(Vu) Then .Item(Vu) = Array(Cl.Offset(, 2).Value, Cl.Offset(, 3).Value, Cl.Offset(, 4).Value)
      Next Cl
      'Pasts all the matching data in Upload tab
      'Loops through the Dictionary Items
      For Each Itm In .Items
         ' checks if the Item has an array
         If IsArray(Itm) Then
            i = i + 1
            'Pastes the values into the sheet
            Sheets("Upload").Range("C" & i).Offset(1, 0).Resize(, 3).Value = Itm
         End If
      Next Itm
   End With
For more info on Dictionaries have a look here https://excelmacromastery.com/vba-dictionary/
The | is simply a separator between the concatenated values. Without it if you had values like
123 456
12 3456
These would both be concatenated to
123456
and would therefore be treated as the same, when they should be treated as different.

HTH
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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