Macro to filter Table to look for duplicates and delete older Entries

Luke1690

Board Regular
Joined
Jul 26, 2022
Messages
125
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi All,

Happy New Year.

Table1 Range A4:AD4 including headers

im looking for a script that will filter my table (Table1) by column D (Header is D4) This is a unique number, to look for duplicates

Then in column F (Header is F4) is the date that data entry was entered in this format DD/MM/YYYY

I want to delete all duplicate old entries. Leaving only the latest input

could this also run when workbook is opened?

This way when the next data set is added the duplicates have already been deleted.

Thanks in advance :)
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi All,

Happy New Year.

Table1 Range A4:AD4 including headers

im looking for a script that will filter my table (Table1) by column D (Header is D4) This is a unique number, to look for duplicates

Then in column F (Header is F4) is the date that data entry was entered in this format DD/MM/YYYY

I want to delete all duplicate old entries. Leaving only the latest input

could this also run when workbook is opened?

This way when the next data set is added the duplicates have already been deleted.

Thanks in advance

Run this on a COPY of your data.

Change the reference to Sheet1 as appropriate.

The table needs to be sorted as the Remove Duplicates line removes all but the last row for each value in column 4.

VBA Code:
Public Sub subDeleteDuplicates()

    With Sheets("Sheet1").ListObjects("Table1")
    
        With .Sort
            .SortFields.Clear
            .SortFields.Add Key:=Columns(4), SortOn:=xlSortOnValues, Order:=xlAscending
            .SortFields.Add Key:=Columns(6), SortOn:=xlSortOnValues, Order:=xlAscending
            .Header = xlYes
            .Apply
        End With
           
        .Range.RemoveDuplicates Columns:=Array(4), Header:=xlYes
    
    End With
    
End Sub
 
Upvote 0
Hi All,

Happy New Year.

Table1 Range A4:AD4 including headers

im looking for a script that will filter my table (Table1) by column D (Header is D4) This is a unique number, to look for duplicates

Then in column F (Header is F4) is the date that data entry was entered in this format DD/MM/YYYY

I want to delete all duplicate old entries. Leaving only the latest input

could this also run when workbook is opened?

This way when the next data set is added the duplicates have already been deleted.

Thanks in advance :)
Please ignore Post 2 and use this code.

VBA Code:
Public Sub subDeleteDuplicates()

    With Sheets("Sheet1").ListObjects("Table1")
    
        With .Sort
            .SortFields.Clear
            .SortFields.Add Key:=Columns(4), SortOn:=xlSortOnValues, Order:=xlAscending
            .SortFields.Add Key:=Columns(6), SortOn:=xlSortOnValues, Order:=xlDescending
            .Header = xlYes
            .Apply
        End With
           
        .Range.RemoveDuplicates Columns:=Array(4), Header:=xlYes
    
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,727
Messages
6,186,685
Members
453,368
Latest member
xxtanka

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