Macro exits unexpectedly. Any Ideas?

dave3944

Board Regular
Joined
Jan 22, 2005
Messages
139
I have a macro which deletes selected worksheets from a workbook. The macro has been working fine for many users for 3 or 4 years. I installed it on a user's PC today that created a problem. Here is the code:

Code:
   Dim i As Integer
   Dim ws As Worksheet

   On Error Resume Next

   Sheets("Sheet1").Range("AA:BB").Delete

   Do
      For i = 3 To Worksheets.Count
         If Sheets(i).Name Like "Step*" Then
            If i > 3 Then
               Application.DisplayAlerts = False
               Sheets(i).Delete
               Application.DisplayAlerts = True
            End If
         ElseIf Sheets(i).Name Like "By Tool*" Or Sheets(i).Name Like "*TWO*" Then
            Application.DisplayAlerts = False
            Sheets(i).Delete
            Application.DisplayAlerts = True
         ElseIf Not Sheets(i).Name Like "*Shipset*" And Not Sheets(i).Name Like "*Number*" Then
            Application.DisplayAlerts = False
            Sheets(i).Delete
            Application.DisplayAlerts = True
         End If
      Next i
   Loop Until Worksheets.Count = 5

The problem is that when I step through the macro, the program exits (ends, stops) at the first Sheets(i).Delete line. Since this has never happened before. I assume it must be an Excel setting. Any ideas?
 
Last edited by a moderator:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
How is the code called, and do you have any code in the sheets you are deleting?
 
Upvote 0
Do we not keep deletion in reverse ordered For Loop like:
Code:
For i = Worksheets.Count To 3 Step -1
'Your code here
Next i
 
Upvote 0
I deleted the "On Error Resume Next" before, thinking it would throw an error, but it didn't. It just stops the Macro as if I had put an "End" statement there. The code is called from a UserForm. There is no code on the Sheets which are being deleted.
 
Upvote 0
Thanks for your help Taurean. In this case, I want to keep the first two sheets and the last two sheets. I just want to delete all the sheets in between.
 
Upvote 0
Is the new user using the same version of Windows and Office as the others? (and which versions are they?)
Are there any controls on those sheets? Is there any code in the ThisWorkbook module of the workbook?
 
Upvote 0
If your requirement is so explicit then you can use this sledgehammer approach:
Code:
Public Sub DeleteSheets()
Dim i As Integer
Application.DisplayAlerts = False
For i = Worksheets.Count - 2 To 3 Step -1
Sheets(i).Delete
Next i
Application.DisplayAlerts = True
End Sub
 
Upvote 0
Great ideas. I hadn't thought of those. But...
All the same version. No controls on the deleted sheets. No code in This Workbook.
Since it's a new user. I may re-install Office 2007. That's about all I can figure out. Something must have been skipped in the original installation.
 
Upvote 0

Forum statistics

Threads
1,224,502
Messages
6,179,126
Members
452,890
Latest member
Nikhil Ramesh

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