• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Help to delete multiple sheets

mohan08

Member
Hi All,

Help required to create macro to delete all sheets excepts on the one sheet which is mentioned via input box. I have tried with the below macro but getting the error message "Run-Time error '438"

Code:
Sub retain()
Dim todelete As Worksheet
Dim Filetodelete As String
Filetodelete = InputBox("Enter file name")

For Each todelete In Application.ActiveWorkbook.Worksheets
If todelete <> "filetodelete.value" Then
todelete.Delete
End If
Next
End Sub
 
Hello Mohan
Try this code
Code:
Sub Retain()
    Dim todelete        As Worksheet
    Dim filetodelete    As String

    filetodelete = InputBox("Enter Worksheet Name")

    Application.DisplayAlerts = False
        For Each todelete In Application.ActiveWorkbook.Worksheets
            If todelete.Name <> filetodelete Then
                todelete.Delete
            End If
        Next todelete
    Application.DisplayAlerts = True
End Sub
 
Thanks a ton Yasser, Can you please help me with combining the below code for unhiding all the worksheet and delete the sheets.

Code:
Sub UnhideAllSheets()

'Unhide all sheets in workbook.

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

ws.Visible = xlSheetVisible

Next ws

End Sub
 
Do you mean like that
Code:
Sub Retain()
    Dim todelete        As Worksheet
    Dim filetodelete    As String

    filetodelete = InputBox("Enter Worksheet Name")

    Application.DisplayAlerts = False
        For Each todelete In Application.ActiveWorkbook.Worksheets
            todelete.Visible = xlSheetVisible
            If todelete.Name <> filetodelete Then
                todelete.Delete
            End If
        Next todelete
    Application.DisplayAlerts = True
End Sub
 
Back
Top