Delete Dupes Macro - Error When No Dupes

JADownie

Active Member
Joined
Dec 11, 2007
Messages
395
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

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
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.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,078
Latest member
skydd

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