Evaluate Time Stamps and Delete Rows with the oldest date per customer

Plokimu77

Board Regular
Joined
Oct 1, 2014
Messages
138
Hello Folks,

Is possible to have a macro evaluate two or more time stamps and delete the oldest one?

In addition, if only one order is found in column A (order for) then ignore and move to the set of orders.

Thank you as always.



This is my raw data

Order ForTime Stamp
Mike9/2/2023 20:07
Mike9/3/2023 20:12
John8/27/2023 00:20
Mary8/28/2023 09:21
Mary8/29/2023 18:20
Kelly8/30/2023 14:51
Melanie7/25/2023 15:00
Melanie8/26/2023 18:21
Melanie8/26/2023 18:25



This would be the Final Result
Order ForTime Stamp
Mike9/3/2023 20:12
John8/27/2023 00:20
Mary8/29/2023 18:20
Kelly8/30/2023 14:51
Melanie08/26/2023 18:25
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
How about this?

WARNING: UNTESTED CODE - TRY ON A COPY OF YOUR WORKBOOK
VBA Code:
Sub RemoveOldTimestamps()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim nameCol As Range
    Dim dateCol As Range
    Dim dict As Object
    Dim i As Long

    Set ws = ThisWorkbook.Worksheets("Sheet1") ' Replace "Sheet1" with your sheet name

    Set nameCol = ws.Range("A:A") 'Adjust columns
    Set dateCol = ws.Range("B:B") 'Adjust columns

    Set dict = CreateObject("Scripting.Dictionary")

    lastRow = ws.Cells(ws.Rows.Count, nameCol.Column).End(xlUp).Row

    For i = lastRow To 2 Step -1
        Dim name As String
        Dim timestamp As Date

        name = nameCol.Cells(i, 1).Value
        timestamp = dateCol.Cells(i, 1).Value
        If dict.Exists(name) Then
            If timestamp > dict(name) Then
                dict(name) = timestamp
            Else
                ws.Rows(i).Delete
            End If
        Else
            dict(name) = timestamp
        End If
    Next i
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top