how to filter to KEEP duplicate records

farful

New Member
Joined
Apr 20, 2009
Messages
3
Hi,
I have a sorted column. I want to remove all singletons from this list, and keep duplicates (and triplicates, quadruplicates, etc).
How can I achieve this?
(VBA method is fine - I'm looking for either something already built into Excel, or something efficient if done in VBA)
Thanks!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Use this macro, it is deleting based on the values in column A (skipping the titles in row1). Change that if you need to:
Code:
Option Explicit
Sub DeleteSingles()
Dim lastrow As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False

    Columns("B:B").Insert Shift:=xlToRight
    Range("B2:B" & lastrow).FormulaR1C1 = "=COUNTIF(R2C1:R26C1,RC[-1])>1"
    Range("A1").AutoFilter
    Range("A1").AutoFilter Field:=2, Criteria1:="=FALSE", Operator:=xlAnd
    Range("A2:B" & lastrow).EntireRow.Delete
    Range("A1").AutoFilter
    Columns("B:B").Delete Shift:=xlToLeft

Application.ScreenUpdating = True
End Sub
 
Upvote 0
Awesome, thanks!

Should change to:
Range("B2:B" & lastrow).FormulaR1C1 = "=COUNTIF(C1:C1,RC[-1])>1"
 
Upvote 0
Actually, this does not take advantage of the fact that my list is already sorted - is there any way to improve the run time using this fact?
 
Upvote 0
No, the autofilter is doing all the heavy lifting in one step, I'm not sure how to improve on THAT. Impatient, are we? Hehe, how many rows of data are we talking about?

Well, let's see if this is any help:
Rich (BB code):
Option Explicit
Sub DeleteSingles()
Dim lastrow As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False

    Columns("B:B").Insert Shift:=xlToRight
    Range("B2:B" & lastrow).FormulaR1C1 = "=COUNTIF(R2C1:R26C1,RC[-1])>1"
        
    Application.Calculation = xlCalculationManual
    Range("A1").AutoFilter
    Range("A1").AutoFilter Field:=2, Criteria1:="=FALSE", Operator:=xlAnd
    Range("A2:B" & lastrow).EntireRow.Delete
    Range("A1").AutoFilter
    Columns("B:B").Delete Shift:=xlToLeft
    Application.Calculation = xlCalculationAutomatic
    
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,884
Messages
6,181,566
Members
453,053
Latest member
Kiranm13

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