Delete Duplicates if value of the cells Count exceeds 3 times

tobysimon

New Member
Joined
Jun 14, 2024
Messages
7
Office Version
  1. 2021
Hello Experts,

Am trying VBA for below execution but it seems to delete the entire data.

Column "R" has duplicates, where each cell value has more than 3 duplicates in the column and some are not. How do I limit the deletion of cells to 3 when found more. Below is example, here only "Count >=4 must be deleted leaving "Guntersville 45481 & Gadsden 45481" with one cell and "Marion 45481" with all 3 cells as is.

1720436563726.png


Really appreciate if I could get some help on this.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Is column S ("Count") just a helper column, or is it a column you need to keep and show?
If you need to continue to show it, should it show the original 4 and 5 you have listed there now, or should it show 1 after the duplicate deletions?
 
Upvote 0
Is column S ("Count") just a helper column, or is it a column you need to keep and show?
If you need to continue to show it, should it show the original 4 and 5 you have listed there now, or should it show 1 after the duplicate deletions?
Thanks for the reply Joe4, it's just a helper. After the deletion it would show as 3. Since 3 is the limit.
 
Upvote 0
It looks like maybe color R is a helper column too.
So you are just looking for duplicate when considering columns P and Q, right?

So based on your example, is this what your expected outcome should look like?
1720448436082.png


If not, please post us what you want your outcome to look like.
 
Upvote 0
It looks like maybe color R is a helper column too.
So you are just looking for duplicate when considering columns P and Q, right?

So based on your example, is this what your expected outcome should look like?
View attachment 113828

If not, please post us what you want your outcome to look like.
Below is the Outcome Joe. I'd need column R to remain a concatenate. Hope it helps

1720448572079.png
 
Upvote 0
Assuming that the counts in column S are the result of a formula and it is not hard-coded, this little VBA procedure should do what you want:
VBA Code:
Sub RemoveDups()

    Dim lr As Long
    Dim r As Long
   
    Application.ScreenUpdating = False
   
'   Find last row in column S with data
    lr = Cells(Rows.Count, "S").End(xlUp).Row
   
'   Loop through all rows backwards deleting duplicates > 3
    For r = lr To 2 Step -1
'       Delete row if value in column S > 3
        If Cells(r, "S") > 3 Then Rows(r).Delete
    Next r
   
    Application.ScreenUpdating = True
   
End Sub
 
Upvote 0
Solution
Assuming that the counts in column S are the result of a formula and it is not hard-coded, this little VBA procedure should do what you want:
VBA Code:
Sub RemoveDups()

    Dim lr As Long
    Dim r As Long
  
    Application.ScreenUpdating = False
  
'   Find last row in column S with data
    lr = Cells(Rows.Count, "S").End(xlUp).Row
  
'   Loop through all rows backwards deleting duplicates > 3
    For r = lr To 2 Step -1
'       Delete row if value in column S > 3
        If Cells(r, "S") > 3 Then Rows(r).Delete
    Next r
  
    Application.ScreenUpdating = True
  
End Sub
Thank you Joe4, it worked. Appreciate your effort.
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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