Remove all duplicates to new sheet

Sharid

Well-known Member
Joined
Apr 22, 2007
Messages
1,066
Office Version
  1. 2016
Platform
  1. Windows
Hi

My code below removes duplicates to a new sheet, however I need it to do a bit more which I can not work out and have been stuck on for a few days.

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

Sheet 1
Has a data table, I want it to find the duplicates in column D and move them and the original record . So the data will show like this in the new Sheet called "Dup" BELOW

From This
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Name
[/TD]
[TD]Age
[/TD]
[/TR]
[TR]
[TD]John
[/TD]
[TD]20
[/TD]
[/TR]
[TR]
[TD]Jane
[/TD]
[TD]20
[/TD]
[/TR]
[TR]
[TD]Peter
[/TD]
[TD]18
[/TD]
[/TR]
[TR]
[TD]Mike
[/TD]
[TD]25
[/TD]
[/TR]
[TR]
[TD]Bob
[/TD]
[TD]17
[/TD]
[/TR]
[TR]
[TD]Sam
[/TD]
[TD]18
[/TD]
[/TR]
[TR]
[TD]David
[/TD]
[TD]26
[/TD]
[/TR]
[TR]
[TD]Mary
[/TD]
[TD]20
[/TD]
[/TR]
[TR]
[TD]Paul
[/TD]
[TD]19
[/TD]
[/TR]
</tbody>[/TABLE]


Sheet2 is called Dup
All duplicate records have been removed along with the original. Duplicates records NOW are to show in RED and Original (First record) to show in Black

To This
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Name
[/TD]
[TD]Age
[/TD]
[/TR]
[TR]
[TD]John
[/TD]
[TD]20
[/TD]
[/TR]
[TR]
[TD]Jane
[/TD]
[TD]20
[/TD]
[/TR]
[TR]
[TD]Mary
[/TD]
[TD]20
[/TD]
[/TR]
[TR]
[TD]Peter
[/TD]
[TD]18
[/TD]
[/TR]
[TR]
[TD]Sam
[/TD]
[TD]18
[/TD]
[/TR]
</tbody>[/TABLE]

Sheet 1
Now looks like this

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Name
[/TD]
[TD]Age
[/TD]
[/TR]
[TR]
[TD]Mike
[/TD]
[TD]25
[/TD]
[/TR]
[TR]
[TD]Bob
[/TD]
[TD]17
[/TD]
[/TR]
[TR]
[TD]David
[/TD]
[TD]26
[/TD]
[/TR]
[TR]
[TD]Paul
[/TD]
[TD]19
[/TD]
[/TR]
</tbody>[/TABLE]


Thanks
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
How about
Code:
Sub sharid()
   Dim Cl As Range, Rng As Range
   Application.ScreenUpdating = False
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("D1:D" & Range("D" & Rows.Count).End(xlUp).Row)
         If Not .Exists(Cl.Value) Then
            .Add Cl.Value, Cl
         Else
            Cl.EntireRow.Font.Color = vbRed
            If Rng Is Nothing Then Set Rng = Union(Cl, .Item(Cl.Value)) Else Set Rng = Union(Rng, Cl, .Item(Cl.Value))
         End If
      Next Cl
   End With
   If Not Rng Is Nothing Then
      Rng.EntireRow.Copy Sheets("Dup").Range("D" & Rows.Count).End(xlUp).Offset(1, -3)
      Rng.EntireRow.Delete
   End If
End Sub
 
Last edited:
Upvote 0
Fluff, this is super. As always you have saved my life once again:rofl:
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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