Home » excel » excel – Delete sheets after certain sheet

excel – Delete sheets after certain sheet

Posted by: admin May 14, 2020 Leave a comment

Questions:

I need to delete all sheets after a sheet called “template 1”.

So far I have tried

Sub SheetKiller()
    Dim i As Long
    Dim j As Long
    j = 0
    For i = 1 To Sheets.Count
        If Sheets(i).Name = "template 1" Then
            j = i
        End If
    Next i

    If j = 0 Or j = Sheets.Count Then Exit Sub

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

However, I get the error message “Delete method of Worksheet class failed”. Anyone got a solution to this?

How to&Answers:

The only thing that I can think of that would throw this error is having a “Very Hidden” sheet in your workbook. “Very Hidden” sheets will not delete using your function. Try this:

Sub SheetKiller()
    Dim i As Long
    Dim j As Long
    Dim bFound As Boolean

    j = 0
    bFound = False

    For i = 1 To Sheets.Count
        If bFound Then Sheets(i).Visible = 0
        If Sheets(i).Name = "template 1" Then
            j = i
            bFound = True
        End If
    Next i

    If j = 0 Or j = Sheets.Count Then Exit Sub

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

This will set all sheets that are going to be deleted to be “Hidden”, allowing them to be deleted without ever displaying them. If the “Very Hidden” sheet needs to remain, then use this:

Sub SheetKiller()
    Dim i As Long
    Dim j As Long

    j = 0
    For i = 1 To Sheets.Count
        If Sheets(i).Name = "template 1" Then
            j = i
        End If
    Next i

    If j = 0 Or j = Sheets.Count Then Exit Sub

    Application.DisplayAlerts = False
        For i = Sheets.Count To j + 1 Step -1
            If Sheets(i).Visible < 2 Then Sheets(i).Delete
        Next i
    Application.DisplayAlerts = True
End Sub

Answer:

Return the Index number of the sheet and then delete all sheets with an index higher than that.

You need to check that “template 1” isn’t the only sheet in the workbook or that it isn’t the last sheet in the workbook. You also need to delete from the last sheet backwards to the first – which I can see you’ve done in your original code.

Sub Test()

    Dim Indx As Long
    Dim x As Long

    With ThisWorkbook
        On Error Resume Next
            Indx = .Sheets("template 1").Index
        On Error GoTo 0

        If Indx <> 0 Then
            If .Sheets.Count > 1 And Indx < .Sheets.Count Then
                Application.DisplayAlerts = False
                For x = .Sheets.Count To Indx + 1 Step -1
                    .Sheets(x).Delete
                Next x
                Application.DisplayAlerts = True
            End If
        End If
    End With

End Sub

Edit: Just realised – it also needs to check that at least one sheet is visible, but guessing that “template 1” would be visible so cheating a bit and not checking for that. 🙂

Edit 2: I’ve added a check to make sure template 1 exists. Usually I’d pass this out to a separate function which would return TRUE/FALSE.

Edit 3: Sorry! Changed Worksheets to Sheets so it includes Chart sheets.

Answer:

Please try

ThisWorkbook.Sheets(i).Delete

instead of Sheets(i).Delete

Answer:

Make sure that you have save the file with enable macros and then try:

Replace:

Sheets(i).Delete

Replace with:

ThisWorkbook.Worksheets(i).Delete