Hello..
I am trying to delete all sheets after sheet 7 but getting errors. From what I am seeing, some sheets do get deleted, but not sequentially (not that I care on sequence, but just an observation) and then I get a Run-time error '9' Subscript out of range error.
I start off with having several sheets hidden that I unhide to make cycling through the sheets easier. Then starting with sheet 7, I want do delete all sheets.
If I run through this several times, eventually all the sheets (starting with sheet 7 on) do get deleted, but I cannot understand what I am doing wrong. My code is as follows:
Dim i As Long
Dim WS As Worksheet
For Each WS In Worksheets
WS.Visible = True
Next
totalsheets = Worksheets.Count
Application.DisplayAlerts = False
For i = 7 To totalsheets
Sheets(i).Delete
Next i
Any guidance would be appreciated
Thank you!
I am trying to delete all sheets after sheet 7 but getting errors. From what I am seeing, some sheets do get deleted, but not sequentially (not that I care on sequence, but just an observation) and then I get a Run-time error '9' Subscript out of range error.
I start off with having several sheets hidden that I unhide to make cycling through the sheets easier. Then starting with sheet 7, I want do delete all sheets.
If I run through this several times, eventually all the sheets (starting with sheet 7 on) do get deleted, but I cannot understand what I am doing wrong. My code is as follows:
Dim i As Long
Dim WS As Worksheet
For Each WS In Worksheets
WS.Visible = True
Next
totalsheets = Worksheets.Count
Application.DisplayAlerts = False
For i = 7 To totalsheets
Sheets(i).Delete
Next i
Any guidance would be appreciated
Thank you!