Update Macro Moving Dupe Records

JADownie

Active Member
Joined
Dec 11, 2007
Messages
395
Hi,

I have the below code which works fine to identify duplicates. Currently it is moving 1 of the 2 duplicate records to the 'Duplicates' sheet but I would actually like it to move both of the duplicate records. I can't quite figure out how to tweak my code to accomplish that so any tips and advice would be greatly appreciated. Thanks!!



Dim Rng As Range, i As Long
Application.ScreenUpdating = False
Set Rng = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
For i = Rng.Rows.Count To 1 Step -1
If Application.WorksheetFunction.CountIf(Rng, Cells(i, "B")) > 1 Then
lr = Sheets("Duplicates").Cells(Rows.Count, "B").End(xlUp).Row + 1
Rows(i).EntireRow.Cut Destination:=Sheets("Duplicates").Range("A" & lr)
Rows(i).EntireRow.Delete
End If
Next i
Application.ScreenUpdating = True
End Sub
 

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
How about
Code:
Sub MoveDupes()
   Dim Ws1 As Worksheet
   Dim Ws2 As Worksheet
   Dim Cl As Range
   Dim Rng As Range
   
   Set Ws1 = Sheets("Sheet1")
   Set Ws2 = Sheets("Sheet2")
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws1.Range("B2", Ws1.Range("B" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then
            .Add Cl.Value, Cl
         Else
            If Rng Is Nothing Then
               Set Rng = Union(Cl, .Item(Cl.Value))
            Else
               Set Rng = Union(Rng, Cl, .Item(Cl.Value))
            End If
         End If
      Next Cl
      If Not Rng Is Nothing Then
         Rng.EntireRow.Copy Ws2.Range("B" & Rows.Count).End(xlUp).Offset(1, -1)
         Rng.EntireRow.Delete
      End If
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

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