VBA - Copy All Duplicate Data (Including first instance)

FeedbackNoob

New Member
Joined
Jul 16, 2019
Messages
1
Hi guys,

I've just started a new role and I need to find all duplicates in data over the past 6 months as my predecessor hadn't been doing this, and the idea of doing it manually for 8000 lines of data is terrifying me. Can anyone help me with VBA code to pull all duplicates into another spreadsheet?

My file looks something like the following, with around 30 columns and 8000 rows of data.

Identifier NumberBusiness NameAccount ManagerBusiness TypeContact ReasonDate of Contact
12345Bobs BaconJohnny JonesFood OutletPricing01/01/2018
82654Sarah's ShellfishJack DanielsFood OutletLack of contact06/04/2018
48572Connors Corner StoreJim SmithConvenienceCustomer service09/08/2018
12345Bobs BaconJohnny JonesFood OutletDamaged stock11/09/2018

<tbody>
</tbody>

What I need is to find all duplicates of the identifier number (even if the rest don't match) which is in Column B in my worksheet, and then pull the whole row of data into another spreadsheet, so I can follow up on why these duplicates exist.

For example in the table above 12345 Bobs Bacon occurs twice with a different contact reason but I would need to pull both of these into another sheet.

I've tried doing this with a pivot table but it doesn't really work for me, and I've found and tweaked some macros online, but they ignore the first instance and just pull those listed after the first. I don't know enough to be sure on how to change this.

Any help, even just pointing me in the right direction on how to do this would be amazing. Thank you in advance :)
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Here's a manual way :
• In a blank column, enter =IF(COUNTIF(A:A,A1)=1,1,2) and fill down
• Sort by this column and all the duplicated ID Nbrs will be at the bottom
 
Upvote 0
Try this. Just make sure to change all the "YOUR NAME" to your worksheet's name

Sub Test()


Dim source As Worksheet, destination As Worksheet
Dim i As Long

Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Duplicate"

Set source = ThisWorkbook.Worksheets("YOUR NAME")
Set destination = ThisWorkbook.Worksheets("Duplicate")

Sheets("YOUR NAME").Activate


Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

Range("B2").Value = "=COUNTIF(A:A,A2)"

Range("B2").Select
Selection.AutoFill destination:=Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)

Columns("B").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

Range("A1").EntireRow.Select
Selection.Copy destination:=Sheets("Duplicate").Range("A1")

For i = 2 To source.Cells(source.Rows.Count, "B").End(xlUp).Row
If source.Range("B" & i).Value > 1 Then
source.Range("A" & i).EntireRow.Copy destination.Range("A" & destination.Cells(destination.Rows.Count, "B").End(xlUp).Row + 1)
source.Range("A" & i).EntireRow.EntireRow.Delete
End If
Next i

Sheets("YOUR NAME").Select
Columns("B:B").Select
Selection.Delete Shift:=xlToLeft

Sheets("Duplicate").Select
Columns("B:B").Select
Selection.Delete Shift:=xlToLeft

End Sub
 
Upvote 0
If a macro is required, it would be more efficient to base it on the steps per post # 2 :
Code:
Sub Test()
Dim source As Worksheet, dest As Worksheet, rng As Range
ThisWorkbook.Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Duplicate"
Set dest = ThisWorkbook.Worksheets("Duplicate")
Set source = ThisWorkbook.Worksheets("YOUR NAME") 'Change name as required
source.Activate
[A:A].Insert
Rows(1).Copy dest.Rows(1)
Set rng = Range("A2:A" & Cells(Rows.Count, "B").End(3).Row)
rng.Formula = "=IF(COUNTIF(B:B,B2)=1,1,""a"")"
With source.Sort
    .SortFields.Clear
    .SortFields.Add2 Key:=rng(1), SortOn:=xlSortOnValues, _
        Order:=xlAscending, DataOption:=xlSortNormal
    .SetRange rng.EntireRow
    .Header = xlNo
    .Apply
End With
With [A:A].SpecialCells(xlCellTypeFormulas, 2).EntireRow
    .Copy dest.[A2]
    .Delete
End With
[A:A].Delete
dest.[A:A].Delete
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,426
Members
448,961
Latest member
nzskater

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