This code compares data from two spreadsheets generated from our clinic's software. The goal is to eliminate all the rows from the PaymentsSheet (our most recent transactions) that are duplicated on the InvoicesSheet (already uploaded transactions) to avoid duplicating info when we upload it to our account software, matching multiple columns to confirm that it is an actual duplicate.
The sheets have a few thousand rows each and will only be getting bigger. Right now it's moving very, very slowly so I'm trying to make my code more efficient with very little success. Any suggestions you have to speed it up or other areas I can improve on would be greatly appreciated. I've included the whole piece just in case but the bottom section with the loops seems to be the big problem area.
Sub cleanInvoices()
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
Application.ScreenUpdating = False
Dim PaymentsWorkbook As Workbook
Set PaymentsWorkbook = ThisWorkbook
Dim PaymentsSheet As Worksheet
Set PaymentsSheet = PaymentsWorkbook.Sheets(4)
Dim wb As Workbook
For Each wb In Application.Workbooks
wb.Save
Next wb
Dim fileLocation As Variant
fileLocation = Application.GetOpenFilename(Title:="Please choose a Excel File to Open", MultiSelect:=False)
If VarType(fileLocation) = vbBoolean Then
MsgBox "No file selected, Please rerun macro", vbExclamation, "No File Selected!"
Exit Sub
End If
Dim InvoicesWorkbook As Workbook
Set InvoicesWorkbook = Workbooks.Open(fileLocation)
Dim InvoicesSheet As Worksheet
Set InvoicesSheet = InvoicesWorkbook.Sheets(1)
Dim InvoiceRow As Integer
Dim PaymentsRow As Integer
Dim lastCellinPaymentsRow As Integer
Dim lastCellinInvoicesSheet As Integer
Dim lastCellinPaymentsSheet As Integer
Dim lastCellinInvoicesRow As Integer
Dim numMatches As Integer
'clears blank rows on sheets (This part seems to move fairly quickly)
PaymentsSheet.Activate
lastCellinPaymentsSheet = Cells(Rows.Count, 1).End(xlUp).Row
PaymentsSheet.Range("A2").Select
PaymentsSheet.Range(Selection, Selection.End(xlDown)).Select
lastCellinPaymentsRow = Selection.Rows.Count + 1
While (lastCellinPaymentsRow <> lastCellinPaymentsSheet)
PaymentsSheet.Range("A" & lastCellinPaymentsRow + 1, "G" & lastCellinPaymentsRow + 1) = PaymentsSheet.Range("A" & lastCellinPaymentsRow, "G" & lastCellinPaymentsRow)
PaymentsSheet.Range("A2").Select
PaymentsSheet.Range(Selection, Selection.End(xlDown)).Select
lastCellinPaymentsRow = Selection.Rows.Count + 1
Wend
PaymentsSheet.Range("A2").Select
PaymentsSheet.Range(Selection, Selection.End(xlDown)).Select
lastCellinPaymentsRow = Selection.Rows.Count
InvoicesSheet.Activate
lastCellinInvoicesSheet = Cells(Rows.Count, 4).End(xlUp).Row
InvoicesSheet.Range("D2").Select
InvoicesSheet.Range(Selection, Selection.End(xlDown)).Select
lastCellinInvoicesRow = Selection.Rows.Count + 1
While (lastCellinInvoicesRow < lastCellinInvoicesSheet)
InvoicesSheet.Range("B" & lastCellinInvoicesRow + 1, "F" & lastCellinInvoicesRow + 1) = InvoicesSheet.Range("B" & lastCellinInvoicesRow, "F" & lastCellinInvoicesRow)
InvoicesSheet.Range("D2").Select
InvoicesSheet.Range(Selection, Selection.End(xlDown)).Select
lastCellinInvoicesRow = Selection.Rows.Count
Wend
'compares sheets and deletes appropriate rows (This part is where it starts to run very slow and lock up)
PaymentsSheet.Activate
For p = 2 To lastCellinPaymentsRow
For i = 2 To lastCellinInvoicesRow
PaymentsSheet.Application.StatusBar = "Completed " & p & " of " & lastCellinPaymentsRow
If StrComp(PaymentsSheet.Cells(p, 1).Value, InvoicesSheet.Cells(i, 4).Value) = 0 Then
If StrComp(PaymentsSheet.Cells(p, 2).Value, InvoicesSheet.Cells(i, 25).Value) = 0 Then
If StrComp(PaymentsSheet.Cells(p, 4).Value, InvoicesSheet.Cells(i, 5).Value) = 0 Then
If StrComp(PaymentsSheet.Cells(p, 7).Value, InvoicesSheet.Cells(i, 42).Value) = 0 Then
numMatches = numMatches + 1
PaymentsSheet.Cells(p, 7).EntireRow.Delete
End If
End If
End If
End If
Next i
Next p
PaymentsSheet.Application.ScreenUpdating = True
End Sub