I have a VBA code that compares values in two columns (Column A & B), which runs slow. Please advise me the changes to make it run fast
The below code works just fine, but is running slow. Please advise me on how to make it run fast
Sub compare()
Dim ListA As Range
Dim ListB As Range
Dim c As Range
Call Unprotect
Set ListA = Range("A2:A1048576")
Set ListB = Range("B2:B1048576")
Application.ScreenUpdating = False
Range("C1").Value = "Files in A that are NOT in B"
Range("D1").Value = "Files in B that are Not in A"
Range("E1").Value = "Matching Files"
For Each c In ListA
If Application.CountIf(ListB, c) = 0 Then
Cells(Cells(Rows.Count, "C").End(xlUp).Row + 1, "C").Value = c
End If
Next c
For Each c In ListA
If Application.CountIf(ListB, c) >= 1 Then
Cells(Cells(Rows.Count, "E").End(xlUp).Row + 1, "E").Value = c
End If
Next c
For Each c In ListB
If Application.CountIf(ListA, c) = 0 Then
Cells(Cells(Rows.Count, "D").End(xlUp).Row + 1, "D").Value = c
End If
Next c
Application.ScreenUpdating = True
Call Protect
End Sub
Any code changes to make the code run fast will be appreciated. Thanks