Delete all sheets after a specific sheetname

betotototo

New Member
Joined
Jul 27, 2011
Messages
4
Hi all,

I'm trying to figure out a vba code that will delete all sheets after a specific sheet NAME. I've found ones that will delete after a specific count (30 in this case):
Code:
Sub DeleteSheets()
    Dim lngLoop As Long
    
    If ThisWorkbook.Worksheets.Count > 30 Then
        Application.DisplayAlerts = False
        For lngLoop = ThisWorkbook.Worksheets.Count To 31 Step -1
            ThisWorkbook.Worksheets(lngLoop).Delete
        Next lngLoop
        Application.DisplayAlerts = True
    End If

End Sub

But for my workbook, I want to delete everything after the sheet "TOI." There could be an infinite amount of sheets before "TOI." Can anyone help me reference that specific sheet and delete everything after it?

Thanks in advance.

PS: Brand spanking new to vba.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi
Welcome to the board

I got confused with the words. You mention you'd like to delete sheets but in the code you only refer to worksheets.

Do you want to delete all sheets after "TOI." or only the sheets type worksheet?
Does your workbook only have sheets type worksheet or does it have also chart sheets?
 
Upvote 0
Try

Code:
Sub DeleteSheets()
Dim i As Long
Application.DisplayAlerts = False
For i = Worksheets.Count To Sheets("TOI.").Index + 1 Step -1
   Worksheets(i).Delete
Next i
Application.DisplayAlerts = True
End Sub
 
Upvote 0
I'm sorry, I didn't realize there was a difference in sheets or worksheets or chart sheets.

The code I reference is just something that I found somewhere on the internet that seemed to do almost what I wanted.

VoG - I get an error: Run-time error'9': Subscript out of range. But after removing the period in "TOI." (I think that might have been a typo?) it worked perfectly!

Thanks guys for your help.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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