Delete selected worksheets

hocs

New Member
Joined
Nov 13, 2009
Messages
33
hi,

I would like to delete from worksheets(5) all the way to the last worksheets, both inclusive.
Please advise code.

Thanks!
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
This should work:
Code:
Dim i As Integer
Dim ws As Worksheet

Application.DisplayAlerts = False
For Each ws In Worksheets
    If i >= 5 Then ws.Delete
    i = i + 1
Next ws

Application.DisplayAlerts = True
 
Upvote 0
thanks misca but this does not select worksheets(5) to the last worksheet of workbook..
 
Upvote 0
Hi hocs,

Not sure why Misca's code doesn't work (looks like it should) but nonetheless here's my attempt:

Code:
Sub Macro1()
    
    Application.ScreenUpdating = False
    
    For Each Worksheet In ActiveWorkbook.Worksheets
        'If the current worksheet has an index number of 5 (i.e. it's the 5th _
        tab from the left [including any hidden sheet(s)], then...
        If Worksheet.Index = 5 Then
            '...turn alerts off and delete the tab.
            Application.DisplayAlerts = False
                Worksheet.Delete 'Note after deleting the Indexed 5th tab, the 6th Indexed tab becomes the 5th Indexed tab and so on.
            Application.DisplayAlerts = True
        End If
    Next Worksheet
    
    Application.ScreenUpdating = True
    
End Sub

HTH

Robert
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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