VBA to remove duplicate rows based on date in other column

harieta

New Member
Joined
Mar 16, 2023
Messages
27
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi all,
I've been going through various threads here trying to adapt the code you create for other users but to no avail.

My table has 11 columns and thousands of rows.
Column B has ID numbers that contains duplicates
I need the code to look at the date (format dd/mm/yyyy) that is in column H and delete the duplicate IDs and keep the row that contains the latest date

I've highlighted what the code should remove

Can you please assist
Thank you
 

Attachments

  • 2023-03-16_16-21-49.jpg
    2023-03-16_16-21-49.jpg
    60.1 KB · Views: 46

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Are you able to sort the data by column B ascending and column H ascending?
 
Upvote 0
Are you able to sort the data by column B ascending and column H ascending?
Raw data is all over the place, with the same ID showing in various rows.
I can do the code do sort one column, but haven't done one that sorts multiple columns
 
Upvote 0
Apologies for the delay. I had to research this and write some code.

Make a backup of your data.
Place this code in a standard code module.
Substutute the word 'Duplicates' on the fourth line for the name of your table.
Make sure that the sheet containing the table is the active sheet.
This code sorts the data by H2 and H8 ascending.

VBA Code:
Public Sub subDeleteDuplicates()
Dim objTable As ListObject

    Set objTable = ActiveSheet.ListObjects("Duplicates")
    
    With objTable.Sort
            .SortFields.Clear
            .SortFields.Add Key:=Range("Duplicates[Header2]"), SortOn:=xlSortOnValues, Order:=xlAscending
            .SortFields.Add Key:=Range("Duplicates[Header8]"), SortOn:=xlSortOnValues, Order:=xlAscending
            .Header = xlYes
            .Apply
        End With
                
    objTable.ListColumns.Add.Name = "Delete"
                               
    Application.AutoCorrect.AutoFillFormulasInLists = True
    objTable.ListColumns("Delete").DataBodyRange.Cells(1).Formula2 = "=IF([@Header2]=B3, ""D"","""")"
    Application.AutoCorrect.AutoFillFormulasInLists = True
        
    objTable.ListColumns("Delete").DataBodyRange.Value = _
        objTable.ListColumns("Delete").DataBodyRange.Value
      
        With objTable.DataBodyRange
            .AutoFilter
            .AutoFilter Field:=.Columns.Count, Criteria1:="D"
            .EntireRow.Delete
            .AutoFilter
        End With
        
        objTable.ListColumns(objTable.DataBodyRange.Columns.Count).Delete

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
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