Speeding the Remove Duplicate Formula

sakrams

Board Regular
Joined
Sep 28, 2009
Messages
59
Office Version
  1. 2016
Platform
  1. Windows
Greeting Excel Gurus,

I have been using below macro to clear duplicates. The best part about his macro is that it clear the dupes but does not delete the rows or sorts the data (it only clear the duplicate values with no rearrangement of cells). However, when I run this formula on 200,000+ rows, excel freezes. I have left the computer for one full day to see if excel comes back but with no luck. Wondering if there is a way to speed this macro?




VBA Code:
Public Sub ClearDuplicateRows()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' DeleteDuplicateRows
' This will delete duplicate records, based on the Active Column. That is,
' if the same value is found more than once in the Active Column, all but
' the first (lowest row number) will be deleted.
'
' To run the macro, select the entire column you wish to scan for
' duplicates, and run this procedure.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim R As Long
Dim n As Long
Dim V As Variant
Dim Rng As Range

On Error GoTo EndMacro
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual


Set Rng = Application.Intersect(ActiveSheet.UsedRange, _
                    ActiveSheet.Columns(ActiveCell.Column))

Application.StatusBar = "Processing Row: " & Format(Rng.Row, "#,##0")

n = 0
For R = Rng.Rows.Count To 2 Step -1
If R Mod 500 = 0 Then
    Application.StatusBar = "Processing Row: " & Format(R, "#,##0")
End If

V = Rng.Cells(R, 1).Value
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Note that COUNTIF works oddly with a Variant that is equal to vbNullString.
' Rather than pass in the variant, you need to pass in vbNullString explicitly.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If V = vbNullString Then
    If Application.WorksheetFunction.CountIf(Rng.Columns(1), vbNullString) > 1 Then
        Rng.Rows(R).Clear
        n = n + 1
    End If
Else
    If Application.WorksheetFunction.CountIf(Rng.Columns(1), V) > 1 Then
        Rng.Rows(R).Clear
        n = n + 1
    End If
End If
Next R

EndMacro:

Application.StatusBar = False
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
MsgBox "Duplicate Rows Deleted: " & CStr(n)

End Sub
 
No error messages, it appears to work perfectly, except it doesn't actually delete the rows. Under what conditions would Excel decide to disobey a direct order?

You are right about the number, I deleted every row that contained anything below the last data row and ran it again, it now stated it deleted 300 rows, which is the correct number, but of course, those rows are still there.
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Forgot there was an error handler in the OP's code try
VBA Code:
Public Sub ClearDuplicateRows()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' DeleteDuplicateRows
' This will delete duplicate records, based on the Active Column. That is,
' if the same value is found more than once in the Active Column, all but
' the first (lowest row number) will be deleted.
'
' To run the macro, select the entire column you wish to scan for
' duplicates, and run this procedure.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim R As Long
Dim n As Long
Dim Ary As Variant
Dim Rng As Range, DelRng As Range

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual


Set Rng = Application.Intersect(ActiveSheet.UsedRange, _
                    ActiveSheet.Columns(ActiveCell.Column))
Ary = Rng.Value

n = 0
With CreateObject("scripting.dictionary")
For R = 2 To UBound(Ary)
   If Not .Exists(Ary(R, 1)) Then
      .Add Ary(R, 1), Nothing
   Else
      If DelRng Is Nothing Then Set DelRng = Rng.Rows(R) Else Set DelRng = Union(DelRng, Rng.Rows(R))
      n = n + 1
   End If
Next R
End With
Rng.Value = Ary
If Not DelRng Is Nothing Then DelRng.EntireRow.Delete

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
MsgBox "Duplicate Rows Deleted: " & CStr(n)
End Sub
 
Upvote 0
Forgot there was an error handler in the OP's code try
VBA Code:
Public Sub ClearDuplicateRows()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' DeleteDuplicateRows
' This will delete duplicate records, based on the Active Column. That is,
' if the same value is found more than once in the Active Column, all but
' the first (lowest row number) will be deleted.
'
' To run the macro, select the entire column you wish to scan for
' duplicates, and run this procedure.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim R As Long
Dim n As Long
Dim Ary As Variant
Dim Rng As Range, DelRng As Range

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual


Set Rng = Application.Intersect(ActiveSheet.UsedRange, _
                    ActiveSheet.Columns(ActiveCell.Column))
Ary = Rng.Value

n = 0
With CreateObject("scripting.dictionary")
For R = 2 To UBound(Ary)
   If Not .Exists(Ary(R, 1)) Then
      .Add Ary(R, 1), Nothing
   Else
      If DelRng Is Nothing Then Set DelRng = Rng.Rows(R) Else Set DelRng = Union(DelRng, Rng.Rows(R))
      n = n + 1
   End If
Next R
End With
Rng.Value = Ary
If Not DelRng Is Nothing Then DelRng.EntireRow.Delete

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
MsgBox "Duplicate Rows Deleted: " & CStr(n)
End Sub

Okay, got the new code and ran it, and got the following error.

1668443723638.png


Then when I hit the Debug button, I got this:

1668443771096.png
 
Upvote 0
Is the data in a proper Table, or just a range?
 
Upvote 0
Is the data in a proper Table, or just a range?

Now that's interesting. The original data is in a table with the filters turned off. I copied and pasted the entire worksheet into a new worksheet and the data is still in a Table but the filters are switched on. So now I have just switched the filters off in the 2nd worksheet and run the script and got the exact same result as I showed you in the reply above. "Delete method of range class failed". I bet you the next thing you suggest will fix it 🙏
 
Upvote 0
Tables are a right pain (IMO) the cons far outweigh the pros.
 
Upvote 0
Tables are a right pain (IMO) the cons far outweigh the pros.

LOL, you have given me an idea because I really wanted this to work for you. Talking about Tables got me thinking, why don't I just delete the duplicates manually, so I did and in an instant the 300 dupes were ancient history 😁 Now I know that's not a VBA fix and I can't use a macro, well I'll try and do a macro but it's unlikely it will record everything I do, actually, I'll try it and let you know. Here's an image with where I went to see the range of my table and spotted the Remove suplicates. I had previously tried that via the Data tab, but going in via the Table Design, just worked.

1668445739182.png
 
Upvote 0
Glad you sorted it & thanks for letting us know.
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,665
Members
449,091
Latest member
peppernaut

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