Sam
Here is the latest code after making the changes that you suggested. I confirm that I have changed the format for columns K & M to Text and that there are no merged cells or Word Wrap
The latest code change that you suggested comes up red with the message compile error syntax error.
Option Explicit
Sub Count90DayOldRecords()
Dim CutoffDate As Date
Dim RecordDate As Date
Dim RecordDates As Range
Dim Sht As Worksheet
Dim StaffNumbers As Range
Dim Cel As Range
Dim LastCel As Range
Dim Found As Range
Dim Answer As Long
Set Sht = Sheets("Sheet1") 'Edit Sheet1 to name of actual worksheet
With Sht
Set LastCel = .Cells(Rows.Count, "H").End(xlUp)
Set RecordDates = Range(.Range("H2"), LastCel) 'Assumes headers in Row 1. Edit if needed.
Set LastCel = .Cells(Rows.Count, "M").End(xlUp)
Set StaffNumbers = Range(.Range("M2"), LastCel) 'Assumes headers in Row 1. Edit if needed.
Set LastCel = Nothing
CutoffDate = DateAdd("D", -126, Date)
'''' Clear previous counts
For Each Cel In StaffNumbers
Cel.Offset(0, 1).ClearContents
Next Cel
''''Start Counting
For Each Cel In RecordDates
'If CDate(Cel.Value) < CutoffDate Then
' ''''Check if valid staff number
'replaced previous two
If DateDiff("d", Cel, Date) > 126 Then
''''Check if valid staff number
' Set Found = StaffNumbers.Find(What:=Cel.Offset(0, 3).Value, After:=.Range("M1")) 'Assumes headers in Row 1. Edit if needed.
'replaced with
Set Found = StaffNumbers.Find(What:=Trim(Cel.Offset(0, 3)), After:=.Range("M1")
If Found Is Nothing Then 'Invalid number
Answer = MsgBox("Staff Number " & Cel.Offset(0, 3) & " not found." & vbCr _
& "Press Yes to continue counting. Press No to stop counting and retry later.")
If Answer = vbNo Then
Exit Sub
Else 'Continue with counting
GoTo LoopNext
End If
Else 'Increment count for this staff number
With Found
.Offset(0, 1).Value = .Offset(0, 1).Value + 1
End With
End If
End If
LoopNext:
Next Cel
End With 'Sht
End Sub