Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cemerson/89b844dacb00a579ab12a867072d2b8f to your computer and use it in GitHub Desktop.
Save cemerson/89b844dacb00a579ab12a867072d2b8f to your computer and use it in GitHub Desktop.
MS Excel - Delete All Worksheets at once but one

In Microsoft Excel, there isn't a built-in feature to delete all worksheets except one directly. However, you can use a VBA (Visual Basic for Applications) macro to achieve this. Here's a step-by-step guide:

Press Alt + F11 to open the VBA editor. In the editor, insert a new module by right-clicking on any item in the Project Explorer, selecting Insert, and then choosing Module. Copy and paste the following VBA code into the module: vba Copy code Sub DeleteAllSheetsExceptOne() Dim ws As Worksheet Dim wsToKeep As Worksheet

' Set the worksheet to keep (replace "SheetToKeep" with the name of the sheet you want to keep)
Set wsToKeep = ThisWorkbook.Sheets("SheetToKeep")

' Loop through all worksheets in the workbook
For Each ws In ThisWorkbook.Sheets
    ' Check if the current sheet is not the one to keep
    If ws.Name <> wsToKeep.Name Then
        ' Delete the sheet
        Application.DisplayAlerts = False ' Disable alerts to avoid confirmation prompt
        ws.Delete
        Application.DisplayAlerts = True ' Enable alerts back
    End If
Next ws

End Sub Replace "SheetToKeep" in the code with the name of the sheet you want to keep. Close the VBA editor. Now, you can run the macro to delete all worksheets except the one you specified. Here's how:

Press Alt + F8 to open the "Macro" dialog. Select DeleteAllSheetsExceptOne and click Run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment