How can I change this code to look for the latest Serviced date and not the earliest:…
- User
How can I change this code to look for the latest Serviced date and not the earliest: Public Sub FindEarliestServiceDate() Application.EnableEvents = False Dim ws As Worksheet Dim lastRow As Long Dim rng As Range Dim cell As Range Dim earliestDate As Date Dim serviceFound As Boolean Set ws = ActiveSheet lastRow = ws.Cells(ws.Rows.count, "B").End(xlUp).Row Set rng = ws.Range("B5:B" & lastRow) For Each cell In rng If cell.Value = "Serviced" Then If earliestDate = 0 Or cell.Offset(0, 6).Value < earliestDate Then earliestDate = cell.Offset(0, 6).Value End If serviceFound = True End If Next cell If serviceFound Then ws.Range("H3").Value = earliestDate Else ws.Range("H3").Value = "" End If Application.EnableEvents = True End Sub
- Assistant
To change the code to look for the latest serviced date instead of the earliest, you can modify the condition in the If statement:
- Change the variable name from
earliestDatetolatestDate. - Update the condition
cell.Offset(0, 6).Value < earliestDatetocell.Offset(0, 6).Value > latestDate.
Here's the modified code:
vba - Change the variable name from