VBA using Excel's MATCH/INDEX function

nvdunn

New Member
Joined
Nov 4, 2015
Messages
12
HI there
I have 2 sets of data and the only unique identifiers I have in both data sets is:

FirstName
LastName
DOB

I am trying to match on these 3 criterias and also return their Address, Suburb, State, POst Code.

This is a very large data set and I am only expecting a small amount of matches however when I use this formula in excel {INDEX(CIDoB,MATCH($AQ2&$AR2&Q2,CIClientFName & CIClientLName & CIDoB,0)))} it just times out and takes FOREVER to update. SO i wanted to create a macro to loop through the data sets, identify the matches and return the names, DOB and addresses.



Address1
Suburb
State
Postcode

I want to do an IF statement that loops through over 400,000 rows that if FIRSTNAME, LASTNAME & DOB match from within my 2 named ranges, return all the data from sheet1 and append to the records on sheet 2 new.

Here is a sample of the Spreadsheet i am trying to loop through. I'm using named ranges for DOB FName LName for both worksheets. To get the one match in records, this is the Excel code i used
Q2 = Firstname on sheet 2
R2 = LastName on Sheet 2
P2 = DOB on Sheet 2
DOB = {INDEX(CADOB,MATCH($Q2&$R2&P2,CAFNAME & CALNAME & CADOB,0)))}
Add= {INDEX(CAAdd,MATCH($Q2&$R2&P2,CAFNAME & CALNAME & CADOB,0)))}
Suburb = {(INDEX(CASub,MATCH($Q2&$R2&P2,CAFNAME & CALNAME & CIDoB,0)))) }
PC= {INDEX(CAPC,MATCH($Q2&$R2&P2,CAFNAME & CALNAME & CADOB,0)))}
State = {INDEX(CAState,MATCH($Q2&$R2&P2,CAFNAME & CALNAME & CADOB,0)))}

I can't work out how to upload my excel spreadsheet any hints?
Here's a screen shot

Mger CodeManagerPhoneMobilePhoneEmailFirstnameSurnameAddressDetailsSuburbStatePostCodePh1MobilePremiumDOB
12345Paul Jones0410000000Joel Smith222 Fake StPHESSANTNSW200004225557775/10/1984
45678SimonSmith0410000001JulieBroidy32 Lee PlST IVESQLD4022042255577817/10/1943
12345John Little0410000002CherishLaman12 Leonard PlMARRICKVILLLENSW2122042255577920/01/1986
222888Noel Stevens0410000003AlishaHayswoth77 Smith DrAPPINNSW256004225557807/03/1971
12348Tanya Hill0410000004AlishaHudson78 Smith DriveAPPINNSW256004225557817/03/1971
12345Paul Jones0410000005RajeevSharima100 Qurnaia RoadPARKVILLENSW217404225557821/07/1965
45678SimonSmith0410000006CherishLevi4/8 Leonard PlMARYNSW2122042255578320/01/1952
12345John Little0410000007BrendanFoley21 Arthur StreetHORNSBYNSW474004225557845/10/1984
222888Noel Stevens0410000008AxelWhiting25 Jersey StDEE WHYNSW209904225557855/10/1984
12348Tanya Hill0410000009JohnnoRuler54 Gould PlaceWAHROONGAQLD474004225557865/10/1984

<colgroup><col><col><col><col><col><col><col><col><col><col span="2"><col><col span="3"></colgroup><tbody>
</tbody>

<tbody>
</tbody>

Sheet 2
Mger CodeManagerMgerLastNameClientIDClientGivenNameClientLastNameEntityNameIsPrimaryAddressLine1AddressLine2SuburbNamePostCodeStateCountryEmailDateOfBirthClient2FirstNameClient2LastNameClient1 Match on DOBAddress1SuburbPostcodeState
111222PaulJones5554443Joel SmithYes222 Fake StreetPHESSANT2000NSWAUSTRALIA5/10/1984JoelSmith
111222PaulJones5554443JulieBroidyYes32 Lee PlaceST IVES4022QLDAUSTRALIA17/10/1943JulieBroidy
111222PaulJones5554443CherishLamanYes12 Leonard PlaceMARRICKVILLLE2122NSWAUSTRALIAyes@yahoo.com20/01/1986CherishLaman
111222PaulJones5554443AlishaHayswothYes77 Smith DriveAPPIN2560NSWAUSTRALIA7/03/1971AlishaHayswoth7/03/197177 Smith DrAPPIN2560NSW
111222PaulJones5554443MARYHAYes54 COLLEGE AVENUEQUAKERS HILL2529NSWAUSTRALIA12/03/1959MARYHA
111222PaulJones5554443WENDYHANSNo5 BRISBANE STREETBRISBANE4000QLDAUSTRALIA13/03/1967WENDYHANS
111222PaulJones5554443WENLIHANo54 JONES AVEBLACKBUTT2529NSWAUSTRALIA13/03/1967WENLIHA
111222PaulJones5554443DANNY WAI KAICATANIAYes10 POLARIS PLACEREEVSBY2222NSWAUSTRALIA9/09/1949DANNYCATANIA
111222PaulJones5554443JOHNEECATAYes10 UPPER BEACH StreetBalgowlah2766NSWAUSTRALIA9/09/1949JOHNEECATA

<colgroup><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>


Many thanks for all your help,
Nina
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi nvdunn,

You have provided many formulae with many Names, but unfortunately it is still not very much clear.

I would like to suggest that you give an example about what do you expect the VBA code to do. You have posted sample data, so you have to give more details about the named ranges that should be compared with and sample data of the expected results.

Also, use references from the sample data you have provided. For example, you have mentioned a field name LASTNAME, which does not exist. I think it is Surname, right?

To post sample file, upload it to any cloud drive (Dropbox, Google Drive, OneDrive, MediaFire…), make it shared and post the link.
 
Upvote 0
try this on your Sheet1

Code:
''''''''''Main Function''''''''''''''''''''By: Edu Alester A. Otarra''''''''''
'''''''''December 2015''''''''''''
Sub EXTA()


Application.ScreenUpdating = False


'Declare variables "DIMS" for easy debudding
Dim DOB As Variant
Dim cname1 As String
Dim cname2 As String
Dim lastrow As Long
Dim i As Long
Dim wsheet As Worksheet
Dim dbase As Worksheet




'Set your variable values
Set dbase = ActiveWorkbook.Sheets("dbase")
Set wsheet = ActiveWorkbook.ActiveSheet
wsheet.Range("A2:J20000").ClearContents
cname1 = wsheet.Range("A1").Value
cname2 = wsheet.Range("B1").Value
lastrow = dbase.Cells(Rows.Count, 1).End(xlUp).Row


    


    DOB = wsheet.Range("C1").Value






'Start Row to Loop
For i = 2 To lastrow
    'Condition before Loop
    If dbase.Cells(i, 6) = cname1 And dbase.Cells(i, 7) = cname2 And dbase.Cells(i, 15) = DOB Then
        With dbase
            .Range(.Cells(i, 8), .Cells(i, 11)).Copy Destination:=wsheet.Range("A2000").End(xlUp).Offset(1, 0)
        End With
    End If
    
'Next Row to Loop
Next i




    
    Range("A1").Select


Application.ScreenUpdating = True


End Sub
 
Upvote 0
or this,
note this is just a sample loop.

Code:
''''''''''Main Function''''''''''''''''''''By: Edu Alester A. Otarra''''''''''
'''''''''December 2015''''''''''''
Sub EXTA()


Application.ScreenUpdating = False


'Declare variables "DIMS" for easy debudding
Dim DOB As Variant
Dim cname1 As String
Dim cname2 As String
Dim lastrow As Long
Dim i As Long
Dim wsheet As Worksheet
Dim dbase As Worksheet


'Set your variable values
Set dbase = ActiveWorkbook.Sheets("dbase")
Set wsheet = ActiveWorkbook.ActiveSheet
wsheet.Range("H2:K20000").ClearContents
cname1 = wsheet.Range("E2").Value
cname2 = wsheet.Range("F2").Value
lastrow = dbase.Cells(Rows.Count, 1).End(xlUp).Row


    DOB = wsheet.Range("G2").Value
    
'Start Row to Loop
For i = 2 To lastrow
    'Condition before Loop
    If dbase.Cells(i, 6) = cname1 And dbase.Cells(i, 7) = cname2 And dbase.Cells(i, 15) = DOB Then
        With dbase
            .Range(.Cells(i, 8), .Cells(i, 11)).Copy Destination:=wsheet.Range("H2000").End(xlUp).Offset(1, 0)
        End With
    End If
    
'Next Row to Loop
Next i
    
    Range("A1").Select


Application.ScreenUpdating = True


End Sub
 
Upvote 0
be sure to make a copy of your XL before running any Macro as theres no Undo after running a macro
 
Upvote 0
Thank you so much for your help ttray33y that works well if I compare line by line. My problem is that I have 2 large data sets and the names will not be in order to match on, hence why I was trying to match the data using an Array range formula.

This is how I tweaked your code:



''''''''''Main Function''''''''''''''''''''By: Edu Alester A. Otarra''''''''''
'''''''''December 2015''''''''''''
Sub EXTA()




Application.ScreenUpdating = False




'Declare variables "DIMS" for easy debudding


Dim DOB As Variant
Dim cname1 As String
Dim cname2 As String
Dim lastrow As Long
Dim i As Long
Dim j As Long
Dim wsheet As Worksheet
Dim dbase As Worksheet




'Set your variable values
Set dbase = ActiveWorkbook.Sheets("dbase")
Set wsheet = ActiveWorkbook.ActiveSheet
'wsheet.Range("H2:K20000").ClearContents
'cname1 = wsheet.Range("E2").Value
'cname2 = wsheet.Range("F2").Value
'DOB = wsheet.Range("P2").Value
lastrow = dbase.Cells(Rows.Count, 1).End(xlUp).Row




j = 2

'Start Row to Loop
For i = 2 To lastrow 'row counter for CI Dbase info
'Condition before Loop
cname1 = wsheet.Cells(j, 5).Value
cname2 = wsheet.Cells(j, 6).Value
DOB = wsheet.Cells(j, 16).Value
If dbase.Cells(i, 6) = cname1 And dbase.Cells(i, 7) = cname2 And dbase.Cells(i, 15) = DOB Then
'MsgBox "MATCH"

With dbase
.Range(.Cells(i, 8), .Cells(i, 11)).Copy Destination:=wsheet.Range("AD2000").End(xlUp).Offset(1, 0)
End With
End If

j = j + 1 'increment to loop through each row
'Next Row to Loop
Next i

Range("A1").Select




Application.ScreenUpdating = True




End Sub


To resolve my issue, do I need to compare the information in 2 temporary arrays? i'm just not sure how i can analyse 2 sets of data when the matches will never occur on the same line. In my example, i made the matches appear in the same order (Stupid example sorry) but in reality I need to compare 2 data sets, of varying rows/columns and the matches will never be in the same row so can I use named ranges to try and match? I could do it in excel by using the excel function DOB = {INDEX(CADOB,MATCH($Q2&$R2&P2,CAFNAME & CALNAME & CADOB,0)))} whereby CADOB = ClientA DOB matching FirstName, LastName and DOB cells.

Thanks again for all your help you've got me thinking. I'm sure this is solveable.
many thanks
Nina
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

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