Please try
Public Sub recordLastMonthMilage()
'#
'# declare private variables
'#
Dim pvt_xls_LastMonth As Excel.Worksheet
Dim pvt_dct_LastMonth As Object
Dim pvt_lng_RowNumber As Long
'#
'# initialise
'#
Set pvt_xls_LastMonth = ThisWorkbook.Worksheets("Last Month Data")
Set pvt_dct_LastMonth = CreateObject("Scripting.Dictionary")
'#
'# build a dictionary holding the milage of last month per car registration
'#
With pvt_xls_LastMonth
For pvt_lng_RowNumber = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
If Not pvt_dct_LastMonth.Exists(.Cells(pvt_lng_RowNumber, "A").Value) Then
pvt_dct_LastMonth.Add .Cells(pvt_lng_RowNumber, "A").Value, .Cells(pvt_lng_RowNumber, "B").Value
End If
Next pvt_lng_RowNumber
End With
'#
'# for each registration of the current month worksheet retrieve the milage submitted in the previous
'# month and record the difference in column 12
'#
With ThisWorkbook.Worksheets("Current Month Data")
For pvt_lng_RowNumber = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
If Not pvt_dct_LastMonth.Exists(.Cells(pvt_lng_RowNumber, "A").Value) Then
.Cells(pvt_lng_RowNumber, "L").Value = "NEW ?"
Else
.Cells(pvt_lng_RowNumber, "L").Value = .Cells(pvt_lng_RowNumber, "B").Value - pvt_dct_LastMonth(.Cells(pvt_lng_RowNumber, "A").Value)
End If
Next pvt_lng_RowNumber
End With
'#
'# housekeeping
'#
Set pvt_dct_LastMonth = Nothing
End Sub
Bookmarks