Remove all duplicates to new sheet

Sharid

Well-known Member
Joined
Apr 22, 2007
Messages
1,064
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
Name
Age
John
20
Jane
20
Peter
18
Mike
25
Bob
17
Sam
18
David
26
Mary
20
Paul
19

<tbody>
</tbody>


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
Name
Age
John
20
Jane
20
Mary
20
Peter
18
Sam
18

<tbody>
</tbody>

Sheet 1
Now looks like this

Name
Age
Mike
25
Bob
17
David
26
Paul
19

<tbody>
</tbody>


Thanks
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
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:ROFLMAO:
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,364
Messages
6,124,509
Members
449,166
Latest member
hokjock

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