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
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: