Delete all sheets between Sheets X and Y

JonoFish

New Member
Joined
Mar 9, 2014
Messages
3
Hello
I have two divider sheets - one named "First_Sheet" and one named "Last_Sheet". In between these sheets are a number of different sheets based on a template (the number changes over time depending on how many "timesheets" have been submitted).

At the start of a VBA macro i simply want to make sure i delete all the existing sheets that are currently between those two sheets. (before the macro adds in new ones)

I thought it would be simple enough but its perplexing me.

Any assistance would be greatly appreciated.
VBA
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi and welcome to the Forum
Try
Code:
Sub MM1()
Dim ws As Worksheet
 Application.DisplayAlerts = False
    For Each ws In Worksheets
        If ws.Name <> "Last_Sheet" And ws.Name <> "First_Sheet" Then

            ws.Delete
        End If
    Next ws
Application.DisplayAlerts = True
End Sub
 
Upvote 0
At the start of a VBA macro i simply want to make sure i delete all the existing sheets that are currently between those two sheets.
The following code should do this...
Code:
Sub DeleteSheetsBetweenFirstAndLastMarkerSheets()
  Dim X As Long
  Application.DisplayAlerts = False
  For X = Sheets("Last_Sheet").Index - 1 To Sheets("First_Sheet").Index + 1 Step -1
    Sheets(X).Delete
  Next
  Application.DisplayAlerts = True
End Sub
 
Upvote 0
Righto thanks RIck - i knew there must be a property to define the sheet order but didnt know what it was - seems worksheets.index is it.

Michael M - THanks for the suggestion - i thikn Rick's code will work better as there may be additional sheets added over time outside the first/last sheet range which i dont want to delete.
Cheers,
J
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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