Button to delete oldest data

KGards7

New Member
Joined
Mar 31, 2022
Messages
9
Office Version
  1. 2016
Platform
  1. Windows
Hi, I have an excel document that new records are constantly added to. What I want is a button that will remove the oldest data from it if it is a duplicate in column A.

So as an example:
Cell A1 contains the text "Reference 123"
Cell A3 contains the text "Reference 123"
Cell A15 contains the text "Reference 123"
Upon clicking the button I want the entire row of A1 and A3 to be deleted only leaving the new record of A15.

Is anyone able to help?
Thank you
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try this
VBA Code:
Sub KGards7()
       Dim lr, k As Long
       lr = Range("A" & Rows.Count).End(xlUp).Row
       Dim dic As Object
       Set dic = CreateObject("Scripting.Dictionary")
       
       For k = lr To 1 Step -1
                If dic.Exists(Range("A" & k).Value) = False Then
                    dic(Range("A" & k).Value) = k
                Else: Range("A" & k).EntireRow.Delete
                End If
       Next k
       
End Sub
 
Upvote 0
Solution
Try this
VBA Code:
Sub KGards7()
       Dim lr, k As Long
       lr = Range("A" & Rows.Count).End(xlUp).Row
       Dim dic As Object
       Set dic = CreateObject("Scripting.Dictionary")
      
       For k = lr To 1 Step -1
                If dic.Exists(Range("A" & k).Value) = False Then
                    dic(Range("A" & k).Value) = k
                Else: Range("A" & k).EntireRow.Delete
                End If
       Next k
      
End Sub
Sorry its taken so long but thank you so much its exactly what I needed!

When running it, due to the extreme amount of data in the spreadsheet it can take sometimes close to an hour to completely remove the duplicates.

Just thinking off the top of my head of a way to speed this up, is there a way I could use the button to extract all duplicates into another tab in the workbook, then run the above code and then copy and paste the remaining data back into the original worksheet?

Of course if there is an easier/simpler way to make this run faster that I haven't though of then please feel free.

Again, thank you so much for all your help and support, it is greatly appreciated!
 
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,829
Members
449,471
Latest member
lachbee

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