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: 41
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
Thank you, will try it later tonight and keep you posted
 
Upvote 0

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.
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
Hi,
I changed the name of table and headers in the code but when I run it it removed all data :).

I tried to removed "table" formatting and now just have data with headers,
how would the code change?
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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