Deleting all added worksheets

Mr. Wizard

Board Regular
Joined
Sep 28, 2005
Messages
67
Before using a macro to create more worksheets that contain charts and data, I would like to clear out any existing worksheets.

I would like to use VBA to delete all worksheets in a range with a sheet index number that is higher than a specified number.

Sheets(1) through Sheets(5) are sheets to preserve because they contain data.

Sheet(6) is the lowest possible sheet index number, but may not exist if the user previously deleted it.

I do not know what the highest sheet number will be, since the user can add or remove worksheets that I need to delete.

'Delete existing charts
Application.DisplayAlerts = False
Countsheets = Application.Sheets.Count
If Countsheets > 5 Then
For Sheetkill = 6 To Countsheets
On Error GoTo ErrHandler
Sheets(Sheetkill).Activate
ActiveWindow.SelectedSheets.Delete
ErrHandler:
Next Sheetkill
End If
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
I think this should work.
Code:
Sub MyDeleteSheets()
'   Deletes all sheets after 5th sheet
 
    Dim myNumSheets As Integer
    Dim i As Integer
    
'   Count all sheets in workbook
    myNumSheets = Sheets.Count
    
'   Delete all sheets after sheet 5
    If myNumSheets > 5 Then
        For i = myNumSheets To 6 Step -1
            Application.DisplayAlerts = False
            Sheets(i).Delete
            Application.DisplayAlerts = True
        Next i
    End If
    
End Sub
 
Upvote 0
Thanks! The code looks similar to another approach I took. I like this solution better though. I will give it a try.
 
Upvote 0
I tried the method you suggested.

When I ran the code, it got error 1004 Delete method of Worksheet class failed.
 
Upvote 0
That's odd, it works for me. Which version of Excel are you using?

Maybe "chart sheets" are handled a little differently. Maybe try incorporating the method you usedm where you activate the sheet first to see if it makes any difference.
Code:
Sub MyDeleteSheets()
'   Deletes all sheets after 5th sheet

    Dim myNumSheets As Integer
    Dim i As Integer
    
'   Count all sheets in workbook
    myNumSheets = Sheets.Count
    
'   Delete all sheets after sheet 5
    If myNumSheets > 5 Then
        For i = myNumSheets To 6 Step -1
            Application.DisplayAlerts = False
            Sheets(i).Activate
            ActiveWindow.SelectedSheets.Delete
            Application.DisplayAlerts = True
        Next i
    End If
    
End Sub
 
Upvote 0
I am using Excel 2007. I looked at the code and it should work from everything I've seen.

I am not trying to delete chart tabs, although the method would be quite similar.
 
Upvote 0
I am using Excel 2007 also. Are any of the sheets protected, or are the moved around or added in a strange order?
 
Upvote 0
Thanks for the effort, but it still bombed out on me. I also tried to select the sheet as well as activate it.
 
Upvote 0
See the questions I asked in my last post.

Will all 5 sheets you are keeping always have the same name? If so, we cn loop throught all sheets and delete the ones that do not have those names.
 
Upvote 0
The worksheets are named dynamically by my program, and I have a list of the data used to generate the names, so that I can cycle through them. However if a user deletes some of the data, that would not work.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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