This VBA code deletes every worksheet in an Excel workbook except one specified sheet, helping you clean up files quickly without manual deletion. It's useful for reports, templates, or automated data processing where only a master sheet must remain. You must have macros enabled and understand that this action is irreversible—always backup your workbook first.
What You'll Need
- Microsoft Excel with macro support enabled (Excel 2010 or later)
- A workbook with multiple sheets
- The name of the sheet you want to keep
- Permission to run macros in your Excel security settings
Step-by-Step
- Open the Excel workbook containing the sheets you want to clean up.
- Press
Alt + F11to open the Visual Basic for Applications (VBA) editor. - In the VBA editor, go to Insert > Module to create a new blank code module.
- Copy and paste the following code into the module window:
Sub DeleteAllSheetsExceptOne()
Dim ws As Worksheet
Dim sheetToKeep As String
' Change this to the exact name of the sheet you want to keep
sheetToKeep = "MasterData"
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> sheetToKeep Then
ws.Delete
End If
Next ws
Application.DisplayAlerts = True
MsgBox "All sheets deleted except " & sheetToKeep, vbInformation
End Sub
- Replace
"MasterData"with the exact name of the sheet you want to preserve (case-sensitive, including spaces and punctuation). - Close the VBA editor by clicking the X in the top-right corner or pressing
Alt + Q. - Save the workbook as a macro-enabled file: File > Save As, then select Excel Macro-Enabled Workbook (*.xlsm) from the file type dropdown.
- Press
Alt + F8to open the Macro dialog box. - Select
DeleteAllSheetsExceptOnefrom the list and click Run. - Wait for the message box to confirm completion. Do not close the workbook until the process finishes.
Common Mistakes
- Typing the sheet name incorrectly—Excel is case-sensitive and requires an exact match, including spaces like "Sales Report" vs. "Sales report".
- Forgetting to save as .xlsm, which disables macros on next open and prevents reuse.
- Running the code without turning off alerts, which causes popup dialogs for each deletion and halts the macro.
- Trying to keep a sheet that is hidden or very hidden—this code works on all sheet types, but if the sheet name is misspelled, it will be deleted anyway.
- Running the code on a workbook with only one sheet, which causes no change but may trigger confusion.
Tips
- Always make a backup copy of your workbook before running this macro—there is no undo.
- Use
MsgBox ws.Nameinside the loop to debug which sheet names Excel sees if deletion fails unexpectedly. - To make the code more flexible, prompt the user for the sheet name using
InputBoxinstead of hardcoding it. - If you frequently use this, assign the macro to a button on the ribbon or Quick Access Toolbar for one-click access.
- Test the macro on a copy of your file first, especially if sheets contain linked formulas or external data connections.
- If you need to preserve multiple sheets, modify the condition to check against an array of names using
Application.Match.
Troubleshooting
- Macro won’t run: Check Excel’s macro security settings: File > Options > Trust Center > Trust Center Settings > Macro Settings. Select Enable all macros temporarily for testing (revert after).
- Runtime error '9': Subscript out of range: The sheet name you entered doesn’t exist. Double-check spelling and case. Open the VBA editor, click in the Immediate Window (
Ctrl + G), and type?ThisWorkbook.Worksheets.Countto verify sheet count. - Macro deletes the wrong sheet: You likely misspelled the sheet name. Use
Debug.Print ws.Namebefore theIfstatement to print all sheet names to the Immediate Window for verification. - Excel crashes or freezes: Close other programs, restart Excel, and ensure the workbook isn’t corrupted. Try opening in Safe Mode (
Ctrlwhile opening Excel). - "All sheets deleted" but one remains missing: The sheet you wanted to keep might have been renamed or deleted before the macro ran. Check the workbook after opening to confirm the sheet is still there.
- Macro runs but nothing changes: The sheet name matches exactly, but no other sheets exist. Verify there are at least two sheets before running.
Frequently Asked Questions
Can I keep more than one sheet using this code?
Yes, but you must modify the code. Instead of comparing to a single name, create an array of sheet names to preserve and use Application.Match to check if the current sheet’s name is in that list.
What happens if I delete the last sheet?
Excel requires at least one worksheet in a workbook. This code prevents deletion of the last sheet by design—if only the keep sheet remains, it won’t be deleted. If you accidentally delete the last sheet, Excel will show an error and stop the macro.
Will this work on protected sheets?
Yes, as long as the sheet is not structure-protected. If the workbook structure is protected (via Review > Protect Workbook), you must unprotect it first before running the macro. The code itself doesn’t remove protection—it only deletes sheets.
Does this affect charts, pivot tables, or linked formulas?
Yes. Deleting a sheet removes all its content, including charts, pivot tables, and formulas that reference it. If other sheets depend on data from sheets being deleted, those references will break and show #REF! errors.
Can I run this on a shared workbook?
No. Shared workbooks disable VBA editing and macro execution. You must unshare the workbook first, run the macro, then re-share if needed.
Is there a way to undo this after running?
No. Excel does not track sheet deletions via VBA in the undo stack. The only recovery method is restoring from a backup copy of the file before the macro ran.