delete sheets after a certain tab

daveyc18

Well-known Member
Joined
Feb 11, 2013
Messages
707
Office Version
  1. 365
  2. 2010
hi,

im wondering if there's a way via vba to delete any sheets to the right of the "summary" tab in my workbook.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
How about
Code:
Sub daveyc18()
   Dim i As Long
   
   Application.DisplayAlerts = False
   For i = Sheets("Summary").Index + 1 To Sheets.Count
      Sheets(i).Delete
   Next i
   Application.DisplayAlerts = True
End Sub
 
Upvote 0
You need to delete backwards from the last sheet to the Summary sheet since after you delete a sheet, the Sheets.Count will get throw off and going forward it won't delete that last sheet. Try this:

Code:
Sub dsheet()
Application.DisplayAlerts = False
Dim i As Integer

For i = Sheets.Count To Sheets("Summary").Index + 1 Step -1
    Sheets(i).Delete
Next i
Application.DisplayAlerts = True
End Sub
 
Upvote 0
You need to delete backwards from the last sheet to the Summary sheet since after you delete a sheet, the Sheets.Count will get throw off and going forward it won't delete that last sheet. Try this:

Code:
Sub dsheet()
Application.DisplayAlerts = False
Dim i As Integer

For i = Sheets.Count To Sheets("Summary").Index + 1 Step -1
    Sheets(i).Delete
Next i
Application.DisplayAlerts = True
End Sub

worked like a charm..thanks
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,877
Members
449,056
Latest member
ruhulaminappu

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