Option Explicit
Sub Compare()
'Sheets
Dim wsDec As Worksheet
Dim wsJun As Worksheet
Dim wsPP As Worksheet
Dim wsPA As Worksheet
Dim wsAP As Worksheet
'Last rows
Dim lrDec As Long
Dim lrJun As Long
Dim lrPP As Long
Dim lrPA As Long
Dim lrAP As Long
'Last coluns
Dim lcDec As Long
Dim lcJun As Long
'Dictionary
Dim dicJun As Object
Dim dicDec As Object
'Utility variables
Dim thisRow As Long
Dim fileNum As String
Dim fileNums()
Dim matchDec()
Dim matchJun()
Dim matchCount As Long
'Get worksheets
Set wsDec = Worksheets("DataDec")
Set wsJun = Worksheets("DataJune")
Set wsPP = Worksheets("PresPres")
Set wsPA = Worksheets("PresAbs")
Set wsAP = Worksheets("AbsPres")
'Copy headers
wsDec.Rows(1).Copy wsPP.Cells(1, 1)
wsDec.Rows(1).Copy wsPA.Cells(1, 1)
wsDec.Rows(1).Copy wsAP.Cells(1, 1)
'Define the last row of the different sheets
lrDec = wsDec.Cells(wsDec.Rows.Count, 2).End(xlUp).Row
lrJun = wsJun.Cells(wsJun.Rows.Count, 2).End(xlUp).Row
lrPP = wsPP.Cells(wsPP.Rows.Count, 2).End(xlUp).Row
lrPA = wsPA.Cells(wsPA.Rows.Count, 2).End(xlUp).Row
lrAP = wsAP.Cells(wsAP.Rows.Count, 2).End(xlUp).Row
'Define last columns
lcDec = wsDec.Cells(1, wsDec.Columns.Count).End(xlToLeft).Column
lcJun = wsJun.Cells(1, wsJun.Columns.Count).End(xlToLeft).Column
' Timer
Dim startTime As Single
startTime = Timer
'Get list of file numbers for June
Set dicJun = CreateObject("Scripting.Dictionary")
fileNums = wsJun.Range(wsJun.Cells(1, 2), wsJun.Cells(lrJun, 2)).Value
'Keep a track of matches and row numbers
ReDim matchJun(1 To lrJun, 1 To 2)
matchJun(1, 1) = "TempHeader1"
matchJun(1, 2) = "TempHeader2"
'Process all the rows in June
For thisRow = 2 To lrJun
fileNum = Trim(wsJun.Cells(thisRow, 2).Value)
If Len(fileNum) Then
'Assume no match for now
dicJun(fileNum) = thisRow
matchJun(thisRow, 1) = thisRow
matchJun(thisRow, 2) = 1
End If
Next thisRow
'Get list of file numbers for December
Set dicDec = CreateObject("Scripting.Dictionary")
fileNums = wsDec.Range(wsDec.Cells(1, 2), wsDec.Cells(lrDec, 2)).Value
'Keep a track of matches and row numbers
ReDim matchDec(1 To lrDec, 1 To 2)
matchDec(1, 1) = "TempHeader1"
matchDec(1, 2) = "TempHeader2"
matchCount = 0
'Process all the rows in December
For thisRow = 2 To lrDec
fileNum = Trim(wsDec.Cells(thisRow, 2).Value)
If Len(fileNum) Then
dicDec(fileNum) = thisRow
matchDec(thisRow, 1) = thisRow
'Check if we already have this in the June list
If dicJun.exists(fileNum) Then
'Record the match
matchDec(thisRow, 2) = 0
matchJun(dicJun(fileNum), 2) = 0
matchCount = matchCount + 1
Else
'No match
matchDec(thisRow, 2) = 1
End If
End If
Next thisRow
'Stop screen from updating to speed things up.
Application.ScreenUpdating = False
Application.EnableEvents = False
'Process December sheet
With wsDec.Range(wsDec.Cells(1, 1), wsDec.Cells(lrDec, lcDec + 2))
.Resize(, 2).Offset(, lcDec).Value = matchDec
.Sort Key1:=.Cells(1, lcDec + 2), Order1:=xlAscending, Header:=xlYes
If matchCount > 0 Then .Resize(matchCount, lcDec).Offset(1).Copy wsPP.Cells(lrPP + 1, 1)
If dicDec.Count > 0 Then .Resize(lrDec - matchCount - 1, lcDec).Offset(matchCount + 1).Copy wsPA.Cells(lrPA + 1, 1)
.Sort Key1:=.Cells(1, lcDec + 1), Order1:=xlAscending, Header:=xlYes
.Resize(, 2).Offset(, lcDec).ClearContents
End With
'Process June sheet
With wsJun.Range(wsJun.Cells(1, 1), wsJun.Cells(lrJun, lcJun + 2))
.Resize(, 2).Offset(, lcJun).Value = matchJun
.Sort Key1:=.Cells(1, lcJun + 2), Order1:=xlAscending, Header:=xlYes
If dicJun.Count > 0 Then .Resize(lrJun - matchCount - 1, lcJun).Offset(matchCount + 1).Copy wsAP.Cells(lrAP + 1, 1)
.Sort Key1:=.Cells(1, lcJun + 1), Order1:=xlAscending, Header:=xlYes
.Resize(, 2).Offset(, lcJun).ClearContents
End With
'Re-enable screen updating.
Application.ScreenUpdating = True
Application.EnableEvents = True
' Report
Debug.Print "Time taken : " & Timer - startTime
End Sub