Delete Dupes Macro - Error When No Dupes

JADownie

Active Member
Joined
Dec 11, 2007
Messages
372
Hi -

I have the below macro that I have been using, and it works perfect to flag and remove duplicates. But when there are no duplicates found on the sheet, I get an error at the line below in red.

I am not sure how I would revise this to account for a scenario when there are no duplicates to remove, and I was hoping that someone might be able to assist me here today.

Thank You SO much in advance!!


Sub G_Delete_Dupes_From_Main()


Dim toDel(), I As Long
Dim RNG As Range, Cell As Long
Set RNG = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)


For Cell = 1 To RNG.Cells.Count
If Application.CountIf(RNG, RNG(Cell)) > 1 Then
ReDim Preserve toDel(I)
toDel(I) = RNG(Cell).Address
I = I + 1
End If
Next
For I = UBound(toDel) To LBound(toDel) Step -1
Range(toDel(I)).EntireRow.Delete

Next I

End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Snakehips

Well-known Member
Joined
May 17, 2009
Messages
5,552
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
JAD,

Maybe try...

Code:
Sub G_Delete_Dupes_From_Main()

Dim toDel(), I As Long
Dim RNG As Range, Cell As Long
Set RNG = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)




For Cell = 1 To RNG.Cells.Count
If Application.CountIf(RNG, RNG(Cell)) > 1 Then
ReDim Preserve toDel(I)
toDel(I) = RNG(Cell).Address
I = I + 1
End If
Next


If I > 0 Then
For I = UBound(toDel) To LBound(toDel) Step -1
Range(toDel(I)).EntireRow.Delete
Next I
End If


End Sub

Hope that helps.
 

WarPigl3t

Well-known Member
Joined
May 25, 2014
Messages
1,609
I don't understand the full extent of your code. I understand some of it. I got lost with all the variables. Here's some code that doeswhat you want.
Code:
[COLOR=#333333]Sub G_Delete_Dupes_From_Main()[/COLOR]
firstRow = 2
lastRow = Range("B" & Rows.Count).End(xlup).Row
i = firstRow
Do Until i > lastRow
     ii = i + 1
     Do Until ii > lastRow
          If Range("B" & i).value = Range("B" & ii).value Then
               Rows(ii).Delete
               ii = ii- 1
               lastRow = Range("B" & Rows.Count).End(xlup).Row
          End If
          ii = ii + 1
     Loop
     i = i + 1
Loop
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,713
Messages
5,833,272
Members
430,201
Latest member
Deepakpilla36

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
Top