I am having trouble writing a macro for comparing multiple columns in multiple sheets (of same excel file). I wrote few but they were taking so long that excel was crashing.
Let's say I have 4 sheets in one same file. Sheet1 with two columns (B and C) and 7000 rows. Sheet2 empty sheet new entries. Sheet3 empty sheet for old entries but with some updated value/info. Sheet4 is a database with 2 columns (A and B) and 22000 rows.
I need to compare Column A from Sheet1 to Column B in Sheet4. If there are completely new entries in Column A sheet1, then copy that entry from Column A sheet1 (and its respective value from Column B sheet1) to a new row (columns A and B) in Sheet2. If there are entries in Column A Sheet1 that are already in Column A sheet4, then compare their respective Column B values. If column A+column B combo from Sheet 1 is in Sheet4 then ignore it. If a Value from Column A Sheet1 is in Column A Sheet4, but their respective Column B values are not matching then copy Column A+Column B from Sheet1 to new row (columns A and B) in Sheet3.
I hope it is clear enough. Due to amount of rows (7000 in Sheet1 to be compared to 20000 in Sheet4) I cannot write a macro that processes everything under a minute.
Any help ?
Edit 1: I used the code suggested by @FaneDuru (Thank You!). but I am encountering an error: "Run-time error '457':This key is already associated with an element of this collection" Is it because I have many repeating values in same columns ?
Edit 2: It seems like "if not dict3.exists" code is not recognized by VBA. When I type ".exists" with smaller letter and jump to another line it is supposed correct it to capital ".Exists", right? It is not doing it.
Edit 3: I did some more testing. I was putting breaks and running the code. When I put the break on this line "If WorksheetFunction.CountIf(rngA4, arr1(i, 1)) > 0 Then", no error happens. When I put the break on one line below "For j = UBound(arr4) To 1 Step -1", the error is happening.
Error is : "Run-time error '457':This key is already associated with an element of this collection"
Private Sub CommandButton1_Click()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Dim arr1, arr2, arr3, arr4, dict2 As Object, dict3 As Object, rngA4 As Range
Dim rngB4 As Range, i As Long, j As Long, lastR1 As Long, lastR4 As Long
lastR1 = Sheet1.Range("A" & Sheet1.Rows.Count).End(xlUp).Row
lastR4 = Sheet4.Range("A" & Sheet4.Rows.Count).End(xlUp).Row
Set rngA4 = Sheet4.Range("A2:A" & lastR4)
Set rngB4 = Sheet4.Range("B2:B" & lastR4)
arr1 = Sheet1.Range("B2:C" & lastR1).Value
arr4 = Sheet4.Range("A2:B" & lastR4).Value
Set dict2 = CreateObject("Scripting.Dictionary")
Set dict3 = CreateObject("Scripting.Dictionary")
For i = UBound(arr1) To 1 Step -1
If WorksheetFunction.CountIf(rngB4, arr1(i, 1)) = 0 Then
dict2.Add arr1(i, 1), arr1(i, 2):
End If
If WorksheetFunction.CountIf(rngA4, arr1(i, 1)) > 0 Then
For j = UBound(arr4) To 1 Step -1
If arr1(i, 1) = arr4(j, 1) Then
If arr1(i, 2) <> arr4(j, 2) Then
If arr1(i, 2) <> arr4(j, 2) Then
dict3.Add arr1(i, 1), arr1(i, 2): Exit For
End If
End If
Next j
End If
Next i
If dict2.Count > 0 Then
arr2 = Application.Transpose(Array(dict2.keys, dict2.Items))
Sheet2.Range("A2").Resize(dict2.Count, 2).Value = arr2
End If
If dict3.Count > 0 Then
arr3 = Application.Transpose(Array(dict3.keys, dict3.Items))
Sheet3.Range("A2").Resize(dict3.Count, 2).Value = arr3
End If
MsgBox "Done!"
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub