VBA to move all duplicates to another tab

KGards7

New Member
Joined
Mar 31, 2022
Messages
9
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I have a button that runs the below code:

Sub KGards7()
Dim lr, k As Long
lr = Range("A" & Rows.Count).End(xlUp).Row
Dim dic As Object
Set dic = CreateObject("Scripting.Dictionary")

For k = lr To 1 Step -1
If dic.Exists(Range("A" & k).Value) = False Then
dic(Range("A" & k).Value) = k
Else: Range("A" & k).EntireRow.Delete
End If
Next k

End Sub

Basically it searches column A locates all duplicates and only keeps the most recent one and deletes the whole row of any of the now outdated duplicates.

My issue is this runs on an Excel document where there are vast quantities of data so to complete it can take on times close to an hour.

Is there a way I can click this button to cut and paste out the duplicates into a new tab, then run the above code on the new tab and then paste the results back into the main tab?

Alternatively if you have another solution so the above code can run faster then I'm open to any and all suggestions.

Thank you, I appreciate your time and help!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
This seems to work. For testing purposes I had my data in A1:K15. You will most likely need to adjust the first line of code to account for how many columns your data has. Also, I am outputting the duplicates to a sheet called `Dupes`. You'll need to adjust that line of code to suit your needs as well.

Here was my initial test data.

XL
ABCDEFGHIJK
11FVNAGUPRJJ
21OBOXUEGLEQ
32RZHZTNWFTL
43AGOFSGBNIX
53OPVAMXRZEC
64HCFEAUCTNW
75DGWJQHFJCW
86LEEWHGJTCK
96XPUDDWSYBP
107YQZEDCYPPO
118FCAPYKLUBO
128IRUKSJHEKG
139NKWQITKDEE
1410WANZSHNSVQ
1510ZRCAODOGWS
Data


Then after running the code.

XL
ABCDEFGHIJK
11OBOXUEGLEQ
23OPVAMXRZEC
36XPUDDWSYBP
48IRUKSJHEKG
510ZRCAODOGWS
Data


And then on the 'Dupes' sheet...

XL
A
11
23
36
48
510
Dupes


Here's the code.

VBA Code:
Sub UNK()
Dim r As Range:         Set r = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row).Resize(, 11)
Dim AR() As Variant:    AR = r.Value2
Dim SD As Object:       Set SD = CreateObject("Scripting.Dictionary")
Dim DP As Object:       Set DP = CreateObject("Scripting.Dictionary")
Dim UNQ As Object:      Set UNQ = CreateObject("System.Collections.ArrayList")

For i = 1 To UBound(AR)
    If Not SD.exists(AR(i, 1)) Then
        SD(AR(i, 1)) = AR(i, 1)
    Else
        UNQ.Add Application.Index(AR, i, 0)
        DP.Add AR(i, 1), 1
    End If
Next i

r.ClearContents
Range("A1").Resize(UNQ.Count, 11).Value = Application.Transpose(Application.Transpose(UNQ.toArray))
Sheets("Dupes").Range("A1").Resize(DP.Count).Value = Application.Transpose(DP.keys)
End Sub
 
Upvote 0
Roughly how many rows of data do you have?
 
Upvote 0
For large amounts of data, try
VBA Code:
Sub KGards7()
   Dim Dic As Object
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, i As Long, NxtCol As Long
   
   Set Dic = CreateObject("Scripting.Dictionary")
   NxtCol = Cells(1, Columns.count).End(xlToLeft).Offset(, 1).Column
   Ary = Range("A2", Range("A" & Rows.count).End(xlUp)).Value2
   ReDim Nary(1 To UBound(Ary), 1 To 1)
   
   For r = 1 To UBound(Ary)
      If Not Dic.Exists(Ary(r, 1)) Then
         Dic.Add Ary(r, 1), Nothing
      Else
         Nary(r, 1) = 1
         i = i + 1
      End If
   Next r
   With Range("A2", Range("A" & Rows.count).End(xlUp)).Resize(, NxtCol)
      .Columns(NxtCol).Value = Nary
      .Sort Cells(2, NxtCol), xlAscending, Header:=xlNo
      .Resize(i).EntireRow.Delete
   End With
End Sub
 
Upvote 0
Had that running the wrong way, it will keep the 1st value not the last.
It should be
VBA Code:
Sub KGards7()
   Dim Dic As Object
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, i As Long, NxtCol As Long
   
   Set Dic = CreateObject("Scripting.Dictionary")
   NxtCol = Cells(1, Columns.count).End(xlToLeft).Offset(, 1).Column
   Ary = Range("A2", Range("A" & Rows.count).End(xlUp)).Value2
   ReDim Nary(1 To UBound(Ary), 1 To 1)
   
   For r = UBound(Ary) To 1 Step -1
      If Not Dic.Exists(Ary(r, 1)) Then
         Dic.Add Ary(r, 1), Nothing
      Else
         Nary(r, 1) = 1
         i = i + 1
      End If
   Next r
   With Range("A2", Range("A" & Rows.count).End(xlUp)).Resize(, NxtCol)
      .Columns(NxtCol).Value = Nary
      .Sort Cells(2, NxtCol), xlAscending, Header:=xlNo
      .Resize(i).EntireRow.Delete
   End With
End Sub
 
Upvote 0
Solution
Have you tried the code in post#5?
 
Upvote 0
Have you tried the code in post#5?
Hi, yep I tried it this morning on the latest scheduled run and it worked absolutely perfectly and worked instantly!

This is absolutely brilliant thank you so much Fluff!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
similar code, get alternative
VBA Code:
Sub test()

    Dim rng As Range, i&, ky$, sec(1 To 3)
    With Application
        sec(1) = .ScreenUpdating: .ScreenUpdating = False
        sec(2) = .EnableEvents: .EnableEvents = False
        sec(3) = .Calculation: .Calculation = xlCalculationManual
    End With

    Set rng = Range("A2:A" & Cells(Rows.Count, 1).End(3).Row)
    With CreateObject("Scripting.Dictionary")
        For i = rng.Count To 1 Step -1
            ky = rng(i).Value
            If Not .exists(ky) Then
                .Item(ky) = ""
            Else
                rng(i).Value = ""
            End If
        Next i
    End With
    
    If WorksheetFunction.CountBlank(rng) Then
        rng.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End If
    
    With Application
        .ScreenUpdating = sec(1)
        .EnableEvents = sec(2)
        .Calculation = sec(3)
    End With
    Beep
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,444
Messages
6,124,893
Members
449,194
Latest member
JayEggleton

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