Restart Sub on condition

Wooders

New Member
Joined
Feb 9, 2021
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
I have a Sub that checks a workbook for duplicate worksheets and deletes them. The problem is that after removing the duplicate I want to restart the sub to make sure there are no more duplicates. I tried to use a command just to go to the Sub start but when the Sub completes it then goes back to where it jumped out of the Sub and gives an Out of Range Error. I also tried just setting my loop counters back to their start values but that didn't work either.
Can anyone help please? My code is -

Sub dupCheck()
Dim sht As Worksheet, i As Integer, j As Integer
'find duplicate sheets by checking Cell C5 using the For Loop
For i = 1 To Sheets.Count - 1
For j = (i + 1) To Sheets.Count
If Sheets(i).Range("C5") = Sheets(j).Range("C5") Then Sheets(j).Delete: dupCheck

Next
Next
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi, @Wooders Welcome to the Forum
'find duplicate sheets by checking Cell C5 using the For Loop
So you use C5 value in each sheet to determine duplicate sheets.
Try this:
VBA Code:
Sub a1161231b()
Dim ws As Worksheet
Dim d As Object

Set d = CreateObject("scripting.dictionary")
d.CompareMode = vbTextCompare
For Each ws In Worksheets
    If Not d.Exists(ws.Range("C5").Value) Then
        d(ws.Range("C5").Value) = Empty
    Else
        ws.Delete
    End If
Next
       
End Sub
 
Upvote 0
Solution
Welcome to the Forum

Akuini's approach avoids your double looping. If you wish, you could modify your approach by looping backwards through the j loop:

For j = Sheets.Count To i + 1 Step -1

This means sheets won't be missed when a sheet is deleted and the subsequent numbering changes. There is no need to have dupCheck calling itself.
 
Upvote 0
Thanks Akuini. I am finding it a bit difficult (as a novice) to understand how your suggestion works (I'm getting there slowly) but it does exactly what I need.
 
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,755
Members
449,049
Latest member
excelknuckles

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