VBA to keep duplicates

Bamh1

New Member
Joined
Oct 7, 2021
Messages
40
Office Version
  1. 2016
Platform
  1. Windows
  2. Mobile
Hello,

Typically the intent is to remove duplicates, an option that is now readily available in Excel. However, I want to do the opposite; I want to keep the duplicates, and remove the single occurrences. I've attached images showing an example in which I need to keep the duplicates in Column A together with their corresponding values in Column B. Does anyone have VBA or other solutions for this?

Thank you,

Shawn
 

Attachments

  • Capture1.PNG
    Capture1.PNG
    5.9 KB · Views: 7
  • Capture2.PNG
    Capture2.PNG
    3.7 KB · Views: 7

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
It looks like you are only considering column A when determining duplicates.
This code will delete all single occurrences.
VBA Code:
Sub DeleteSingles()

    Dim lr As Long
    Dim r As Long
   
    Application.ScreenUpdating = False
   
'   Find last row with data in column A
    lr = Cells(Rows.Count, "A").End(xlUp).Row
   
'   Populate column C with counts of each column A value
    Range("C2:C" & lr).FormulaR1C1 = "=COUNTIF(C[-2],RC[-2])"
    Range("C2:C" & lr).Value = Range("C2:C" & lr).Value
   
'   Loop through all rows backwards
    For r = lr To 2 Step -1
'       Delete row in column C is 1
        If Cells(r, "C") = 1 Then Rows(r).Delete
    Next r
       
'   Delete temporary column C
    Columns("C:C").Delete
   
    Application.ScreenUpdating = True
   
End Sub
 
Upvote 0
Solution
Hi,
untested but another way - maybe

VBA Code:
Sub RemoveSingles()
    Dim lr                  As Long
    Dim RemoveSingle        As Range
    
    lr = Range("A1").CurrentRegion.Rows.Count
    
    Application.ScreenUpdating = False
    For r = 2 To lr
        If Application.CountIf(Columns(1), Cells(r, 1).Value) = 1 Then
            If RemoveSingle Is Nothing Then
                Set RemoveSingle = Cells(r, 1)
            Else
                Set RemoveSingle = Union(RemoveSingle, Cells(r, 1))
            End If
        End If
    Next r
    
    If Not RemoveSingle Is Nothing Then RemoveSingle.EntireRow.Delete
    
    Application.ScreenUpdating = True
    
End Sub

code assumes sheet is active & unprotected

Dave
 
Upvote 0
It looks like you are only considering column A when determining duplicates.
This code will delete all single occurrences.
VBA Code:
Sub DeleteSingles()

    Dim lr As Long
    Dim r As Long
 
    Application.ScreenUpdating = False
 
'   Find last row with data in column A
    lr = Cells(Rows.Count, "A").End(xlUp).Row
 
'   Populate column C with counts of each column A value
    Range("C2:C" & lr).FormulaR1C1 = "=COUNTIF(C[-2],RC[-2])"
    Range("C2:C" & lr).Value = Range("C2:C" & lr).Value
 
'   Loop through all rows backwards
    For r = lr To 2 Step -1
'       Delete row in column C is 1
        If Cells(r, "C") = 1 Then Rows(r).Delete
    Next r
  
'   Delete temporary column C
    Columns("C:C").Delete
 
    Application.ScreenUpdating = True
 
End Sub
Thank you so much, Joe. It works.
Much appreciated
 
Upvote 0
You are welcome.
Glad we were able to help!
 
Upvote 0
It looks like you are only considering column A when determining duplicates.
This code will delete all single occurrences.
VBA Code:
Sub DeleteSingles()

    Dim lr As Long
    Dim r As Long
 
    Application.ScreenUpdating = False
 
'   Find last row with data in column A
[QUOTE="Joe4, post: 5950981, member: 6721"]
It looks like you are only considering column A when determining duplicates.
This code will delete all single occurrences.
[CODE=vba]Sub DeleteSingles()

    Dim lr As Long
    Dim r As Long
  
    Application.ScreenUpdating = False
  
'   Find last row with data in column A
    lr = Cells(Rows.Count, "A").End(xlUp).Row
  
'   Populate column C with counts of each column A value
    Range("C2:C" & lr).FormulaR1C1 = "=COUNTIF(C[-2],RC[-2])"
    Range("C2:C" & lr).Value = Range("C2:C" & lr).Value
  
'   Loop through all rows backwards
    For r = lr To 2 Step -1
'       Delete row in column C is 1
        If Cells(r, "C") = 1 Then Rows(r).Delete
    Next r
      
'   Delete temporary column C
    Columns("C:C").Delete
  
    Application.ScreenUpdating = True
  
End Sub
Hi,
untested but another way - maybe

VBA Code:
Sub RemoveSingles()
    Dim lr                  As Long
    Dim RemoveSingle        As Range
   
    lr = Range("A1").CurrentRegion.Rows.Count
   
    Application.ScreenUpdating = False
    For r = 2 To lr
        If Application.CountIf(Columns(1), Cells(r, 1).Value) = 1 Then
            If RemoveSingle Is Nothing Then
                Set RemoveSingle = Cells(r, 1)
            Else
                Set RemoveSingle = Union(RemoveSingle, Cells(r, 1))
            End If
        End If
    Next r
   
    If Not RemoveSingle Is Nothing Then RemoveSingle.EntireRow.Delete
   
    Application.ScreenUpdating = True
   
End Sub

code assumes sheet is active & unprotected

Dave
I tested it on my sample data. This one works too. Thank you so much
[/QUOTE]

lr = Cells(Rows.Count, "A").End(xlUp).Row

' Populate column C with counts of each column A value
Range("C2:C" & lr).FormulaR1C1 = "=COUNTIF(C[-2],RC[-2])"
Range("C2:C" & lr).Value = Range("C2:C" & lr).Value

' Loop through all rows backwards
For r = lr To 2 Step -1
' Delete row in column C is 1
If Cells(r, "C") = 1 Then Rows(r).Delete
Next r

' Delete temporary column C
Columns("C:C").Delete

Application.ScreenUpdating = True

End Sub[/CODE]
 
Upvote 0

Forum statistics

Threads
1,215,632
Messages
6,125,909
Members
449,274
Latest member
mrcsbenson

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