i dont duplicate the error. works fine for me with the uploaded file.
what error message do you get?
sorting the range on helper column before deleting the rows will speed up the code.
i added another helper column to keep the row order.
if you are sure all values in column A are dates, deleting the first condition (If IsDate(.Cells(i, 1)) Then) will also speed up the code.
Sub Final_Cleanup()
Dim StartDate As Date, EndDate As Date
Dim i As Long, calc As Long, LastRow As Long, LastCol As Long
Dim PasteRange As Range
With Application
.DisplayAlerts = False
.ScreenUpdating = False
.EnableEvents = False
calc = .Calculation
.Calculation = xlCalculationManual
End With
StartDate = DateSerial(Year(Date - 20), Month(Date - 20), Day(Date - 20))
EndDate = DateSerial(Year(Date), Month(Date), Day(Date))
On Error Resume Next
Worksheets("Archive").Delete
On Error GoTo 0
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Archive"
With Worksheets("Invoice")
LastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
LastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
.Cells(1, LastCol + 1).Value = "Mark"
.Cells(1, LastCol + 2).Value = "Seq No"
For i = 2 To LastRow
.Cells(i, LastCol + 2).Value = i
If IsDate(.Cells(i, 1)) Then
If CLng(.Cells(i, 1).Value) < CLng(StartDate) Then
Set PasteRange = Worksheets("Archive").Cells(Rows.Count, 1).End(xlUp).Offset(1)
PasteRange.Resize(, LastCol).Value = .Range(.Cells(i, 1), .Cells(i, LastCol)).Value
.Cells(i, LastCol + 1).Value = "Del"
End If
End If
Next i
.UsedRange.Sort Key1:=.Cells(2, LastCol + 1), Order1:=xlAscending, Header:=xlYes
.Cells(1).AutoFilter Field:=LastCol + 1, Criteria1:="=Del"
.UsedRange.Columns(1).Offset(1).SpecialCells(12).EntireRow.Delete '12 = xlCellTypeVisible
.AutoFilterMode = False
.UsedRange.Sort Key1:=.Cells(2, LastCol + 2), Order1:=xlAscending, Header:=xlYes
.UsedRange.Columns(LastCol + 1).ClearContents
.UsedRange.Columns(LastCol + 2).ClearContents
End With
With Application
.EnableEvents = True
.Calculation = calc
End With
End Sub