deleting work-sheets after a certain number

sieger007

New Member
Joined
Mar 9, 2011
Messages
4
Hi Folks
I get a report that has about 17 worksheets . I'd like to keep just the 1st three worksheets. None of the worksheets are hardcoded in their name. e.g. the 1st worksheet by default would be sheet1 but its say <date>.sheet1.
I'd like to have a macro that'll delete ALL the worksheets except the 1st three.
Thanks
Sam
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try this

Code:
Sub DeleteSheets()
Dim i As Long
Application.DisplayAlerts = False
For i = Worksheets.Count To 4 Step -1
    Worksheets(i).Delete
Next i
Application.DisplayAlerts = True
End Sub
 
Upvote 0
Hi there,

Try the following which will delete any tab(s) past the third i.e. the first three from left to right including any hidden tab(s) will not be deleted:

Code:
Sub DeleteAllButFirstThree()

    'http://www.mrexcel.com/forum/showthread.php?t=560698
    
    Dim strArray() As String 'Declares a dynamic array variable
    Dim intArrayCount As Integer
    
    If ActiveWorkbook.Worksheets.Count < 4 Then
        MsgBox "There are only " & ActiveWorkbook.Worksheets.Count & " tabs in the workbook!!", vbExclamation, "Delete tab Editor"
        Exit Sub
    End If
    
    intArrayCount = 0
    
    Application.ScreenUpdating = False
    
    For Each Worksheet In ActiveWorkbook.Worksheets
        If Worksheet.Index > 3 Then
            intArrayCount = intArrayCount + 1
            ReDim Preserve strArray(1 To intArrayCount) 'Copy elements from the existing array to the new array
            strArray(intArrayCount) = Worksheet.Name
        End If
    Next
    
    Application.DisplayAlerts = False
        ActiveWorkbook.Worksheets(strArray).Delete
    Application.DisplayAlerts = True
    
    Erase strArray() 'Deletes the varible contents to free some memory
    
    Application.ScreenUpdating = True
    
End Sub

Note, as the code will delete tabs without asking any questions, make sure you initially run the macro on a copy of your workbook in case the results are not as expected.

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,917
Members
452,949
Latest member
beartooth91

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