Hi, I want to match records in one sheet to another to identify which records don't exist in the second sheet (as opposed to already existing there) so I can add them to that sheet. I've written the following code which is basically two loops looking for a match on the first field and then the second field. Is there a better approach to this than using loops? I have previously use the FIND functionality which I understand to be faster than loops but couldn't work out how to use it with 2 criteria. I've also appended a file with the code in.Looping to find matching values.xlsm Thanks for any ideas, regards, Neil
Public Sub TransferDataToMemDataLog()
Dim FinalRow As Single 'number variables available throughout this module
Dim i As Integer
Dim j As Long
Dim oSht As Worksheet
Dim oSht1 As Worksheet 'object variable for "MemDataLog"
Dim lastRow As Long
Dim strSearch As String 'Lookup value of members number for use on membership sheet
Dim str2Search As String 'Lookup value of members number for use on membership sheet
Dim t As Integer
Set oSht = Sheets("Data") 'make sheet active in memory
Set oSht1 = Sheets("MemDataLog") 'make sheet active in memory
FinalRow = oSht.Cells(Rows.Count, 1).End(xlUp).Row 'Count last row on "Data" sheet
lastRow = oSht1.Cells(Rows.Count, 1).End(xlUp).Row 'Count last row on "MemDataLog" sheet
Set oSht = Sheets("Data") 'set object variable to "Data"
Set oSht1 = Sheets("MemDataLog") 'set object variable to "MemDataLog"
'------------------------------------------------------------------------------------------------
'Loop through "Data" sheet 1 record at a time. Take Name value from "Data" sheet and loop through Name
'values on "MemDataLog" to find match. When find match then check if date also matches. If so,
'it is a duplicate and doesn't need to be added to the "MemDataLog" sheet. If no match then add it.
For i = 2 To FinalRow 'Loop through each record on "Data"
t = 0 'Set variable values
strSearch = oSht.Cells(i, 1).Value
str2Search = oSht.Cells(i, 4).Value
For j = 2 To lastRow 'Loop through each record on "MemDataLog"
If oSht1.Cells(j, 4).Value = str2Search Then 'Check for match on Date field
If oSht1.Cells(j, 1).Value = strSearch Then 'Check for match on Name field
t = 1
Exit For
End If
End If
Next j
If t = 0 Then 'If t remains 0 then no match has been found and record needs to be added
MsgBox str2Search & "on " & strSearch & " " & "has no match, add to MemDataLog sheet"
End If
Next i
End Sub
Bookmarks