Delete Specific Cells based on Count

Anthony1432

New Member
Joined
Feb 1, 2011
Messages
3
Hi all, I'm after a macro that deletes all cells in column A, if they are duplicated more than 2 times (including the original 2).

For example:
Before:
A
1
2
2
3
3
3
3
4
4
4
After:
A
1
2
2

If tried a number of different loops but cant seem to write something that can search for multiple cell value counts above 2, and then delete them.

Thanks!
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Try:
Code:
Sub RemoveCells()
Dim rng As Range, Lrw As Long, delRws As Range
Lrw = Range("A" & Rows.Count).End(xlUp).Row
Set rng = Range("A1", "A" & Lrw)
For Each c In rng
    If WorksheetFunction.CountIf(rng, c.Value) > 2 Then
        If delRws Is Nothing Then
            Set delRws = c
        Else
            Set delRws = Union(delRws, c)
        End If
    End If
Next c
If delRws Is Nothing Then
    MsgBox "No Column A entry is repeated more than twice"
Else
    delRws.Delete shift:=xlUp
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,308
Members
452,904
Latest member
CodeMasterX

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