Fix this

PHOTO EMBED

Wed Dec 06 2023 13:06:32 GMT+0000 (Coordinated Universal Time)

Saved by @shabih #vba

Sub RemoveDuplicatesAndKeepLatest()
    Dim ws As Worksheet
    Set ws = [thisworkbook](/activeworkbook-thisworkbook/).Sheets("YourSheetName") ' Assign [Worksheet](/vba/sheets-worksheets) object to "ws". Replace "YourSheetName" with the actual sheet name

    ' Assuming Column D contains the timestamp
    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row  ' Find the last row in the worksheet

    ' Perform sort operation based on columns 1,3 and timestamp in Column D (adjust accordingly)
    With ws.Sort
        .SortFields.Clear  
        .SortFields.Add Key:=ws.Range("A2:A" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal   ' Sort on column A ascendingly
        .SortFields.Add Key:=ws.Range("C2:C" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal   ' Sort on column C ascendingly
        .SortFields.Add Key:=ws.Range("D2:D" & lastRow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal  ' Sort on column D descendingly (to keep the latest)
        .SetRange ws.Range("A1:AD" & lastRow) 
        .Header = xlYes  
        .MatchCase = False  
        .Orientation = xlTopToBottom  
        .SortMethod = xlPinYin  
        .Apply  ' Apply the sorting to the range
    End With

    ' Loop through the sorted range and remove duplicates, keeping the latest occurrence
    Dim currentRow As Long
    For currentRow = lastRow To 3 Step -1 ' Loop from the bottom of sheet to the top
        If ws.Cells(currentRow, 1).Value = ws.Cells(currentRow - 1, 1).Value And _
           ws.Cells(currentRow, 3).Value = ws.Cells(currentRow - 1, 3).Value Then
            ' If corresponding cells in Column A and Column C are identical, delete the current row
            ws.Rows(currentRow).Delete  ' Delete the row
        End If
    Next currentRow
End Sub
content_copyCOPY

The code you've posted appears to be correct. It sorts a Sheet based on columns A, C, and D, and then loops backwards from the last row to the third row, deleting rows that have duplicate values in columns A and C. Just ensure that: You replace "YourSheetName" with your actual sheet name. The timestamp is in Column D. Your data spans to Column AD. The subroutine begins by declaring and assigning a Worksheet object. The worksheet assignment should be updated to use the desired worksheet's name. The function then determines the last row of the worksheet. Following this, the function performs a nested sorting operation: first sorting by Column A (ascending), then by Column C (ascending), and finally by Column D (descending). This last sort operation on Column D ensures that the most recent entry (assuming the timestamp is in Column D) will be kept when duplicate values are removed. The function then iterates over the rows of the sorted range (starting from the bottom and moving upward). It checks if the cells in Column A and Column C in the current row and the row above are identical. If they are, the function deletes the current row, thus removing duplicates while keeping the most recent occurrence.