lezawang
Well-known Member
- Joined
- Mar 27, 2016
- Messages
- 1,805
- Office Version
- 2016
- Platform
- Windows
Hi
I want to delete all the sheets in a workbook except the first one. I wrote the code below but what is the problem, It deleted some but then I got message "subscript out of range". I think because I do not have 40 sheets (my For loop is from 2 to 40). I do not know how many sheets in the file. I want to delete them no matter how many. Is that possible? thank you.
Sub delelesheets()
Dim x As Integer
For x = 2 To 4
ThisWorkbook.Worksheets(x).Delete
Next
End Sub
I want to delete all the sheets in a workbook except the first one. I wrote the code below but what is the problem, It deleted some but then I got message "subscript out of range". I think because I do not have 40 sheets (my For loop is from 2 to 40). I do not know how many sheets in the file. I want to delete them no matter how many. Is that possible? thank you.
Sub delelesheets()
Dim x As Integer
For x = 2 To 4
ThisWorkbook.Worksheets(x).Delete
Next
End Sub