VBA delete the first 100 duplicates

Mr Tran

New Member
Joined
Nov 16, 2018
Messages
9
Hi,

I need a VBA code to delete the first 100 duplicates in column A and keep the remaining. There could be a thousand names that appear a thousand times, but would want to delete the first 100 duplicates that their name appear on this list and keep the remaining.

Thanks,

Mr Tran
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Something like

Code:
Dim DelRange as Range
Dim DelCount as Long
Dim OneCell as Range

With Range("A:A")
    Set DelRange = .Cells(Rows.Count, 2)
    For Each oneCell in Range(.Cells(1,1), .Cells(Rows.Count,1).End(xlup))
        If WorksheetFunction.CountIf(oneCell, .Cells) <> 1 Then
            DelCount = DelCount + 1
            Set DelRange = Application.Union(DelRange, oneCell)
        End If
        If DelCount >= 100 Then Exit For
    Next oneCell

    On Error Resume Next
    Application.Intersect(DelRange, .Cells).Delete shift:=xlUp
    On Error Goto 0 
End With
 
Last edited:
Upvote 0
Thank you mikerickson for the response. I get a Run-time error '13'; Type mismatch


If WorksheetFunction.CountIf(OneCell, .Cells) <> 1 Then

any help resolving this is very much appreciated.


Thanks!

Mr Tran
 
Upvote 0
I got the arguments backwards

Code:
If WorksheetFunction.CountIf(.Cells, oneCell.Value) <> 1 Then
 
Upvote 0
The code is great but I don't think I was clear. I was looking for something that would delete the first 100 duplicates of each subset of names. If Mary appear 1000 times, Jim appears 900 time and John appear 800 times, the macro would remove 100 duplicates from each.

Thanks!

Mr Tran
 
Upvote 0
Sorry, i phrased the it wrong. I should have said to delete the first 100 instances, so in that situation of Mary appearing 99 times, it would delete all 99
 
Upvote 0
If Mary appears 99 times, delete all of them so Mary is not in the result at all?
Or leave one Mary in the result?
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,982
Members
449,201
Latest member
Lunzwe73

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