Delete Excel Worksheets Except Selected Sheets with VBA
Question details
Delete every worksheet in a workbook except the sheet names listed in a table.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- The workbook contains a table on HIDDEN_DEV_SHEET that identifies worksheets to preserve while all unlisted worksheets should be removed.
- Observed behavior
- The required result is that only worksheets found in the keep-list, including HIDDEN_DEV_SHEET when needed, remain in the workbook.
Confirm that the keep-list table includes every worksheet you need to retain, including HIDDEN_DEV_SHEET if it must remain. Run the macro from a macro-enabled workbook and make sure the workbook has at least one worksheet that will not be deleted.
Delete unlisted worksheets by reading the keep-list table
Use this approach when worksheet names are maintained dynamically in the TableDontDel table on HIDDEN_DEV_SHEET.
The macro searches the table for each worksheet name and deletes a worksheet only when no exact match is found. Looping from the last worksheet to the first prevents collection-index issues while sheets are being removed.
On HIDDEN_DEV_SHEET, create or confirm an Excel table named TableDontDel. Put each worksheet name to preserve in the table's first column, including HIDDEN_DEV_SHEET if that sheet must not be deleted.
In Excel, press Alt+F11, then select Insert > Module from the Visual Basic Editor menu.
Paste this code into the module: Sub DeleteUnlistedSheets() Dim keepRange As Range Dim foundCell As Range Dim i As Long On Error GoTo CleanUp Set keepRange = ThisWorkbook.Worksheets("HIDDEN_DEV_SHEET").ListObjects("TableDontDel").DataBodyRange If keepRange Is Nothing Then Err.Raise vbObjectError + 1, , "TableDontDel has no worksheet names." Application.ScreenUpdating = False Application.DisplayAlerts = False For i = ThisWorkbook.Worksheets.Count To 1 Step -1 Set foundCell = keepRange.Find(What:=ThisWorkbook.Worksheets(i).Name, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False) If foundCell Is Nothing Then ThisWorkbook.Worksheets(i).Delete Next i CleanUp: Application.DisplayAlerts = True Application.ScreenUpdating = True If Err.Number <> 0 Then MsgBox Err.Description, vbExclamation End Sub
Place the cursor inside DeleteUnlistedSheets and press F5, or return to Excel and choose Developer > Macros > DeleteUnlistedSheets > Run.

Keep a fixed set of worksheets with Select Case
Use this simpler macro when the worksheet names to retain are fixed and do not need to be read from a table.
Manage Workbook Worksheets with WPS Spreadsheet Macros
WPS Spreadsheet provides a familiar spreadsheet workspace for organizing worksheets and running compatible VBA-style macro workflows. Use its Developer tools to create a keep-list, open the macro editor, and run worksheet-management automation in a macro-enabled workbook.
- 1. Open the workbook in WPS Spreadsheet: Launch WPS Office, choose WPS Spreadsheet, and open the Excel workbook containing the worksheets and keep-list table.
- 2. Confirm the keep-list entries: Open HIDDEN_DEV_SHEET and verify that the TableDontDel table contains every worksheet name that should remain.
- 3. Open the macro tools: Go to the Developer tab and select Visual Basic or Macro to access the workbook's macro project.
- 4. Add or review the macro: Insert a module if needed, paste the worksheet deletion macro, and confirm that the table and worksheet names match the workbook.
- 5. Run the worksheet cleanup macro: Use Developer > Macros, select the macro, and choose Run after confirming the list of worksheets to keep.

Frequently Asked Questions
Why should the macro loop through worksheets backward when deleting sheets?
Use For i = ThisWorkbook.Worksheets.Count To 1 Step -1 because deleting a worksheet changes the Worksheets collection. Reverse looping avoids skipping worksheets or using an invalid index.
What happens if a worksheet name is missing from TableDontDel?
The macro finds no exact match with Range.Find and deletes that worksheet. Add every sheet you need to retain to HIDDEN_DEV_SHEET's TableDontDel table before running it.
Can the macro delete HIDDEN_DEV_SHEET itself?
Yes. It is treated like any other worksheet, so add HIDDEN_DEV_SHEET to TableDontDel or include it in the Select Case keep list if it must remain.
Why does the macro turn off DisplayAlerts?
Application.DisplayAlerts = False suppresses the confirmation dialog that Excel normally shows for every worksheet deletion. The cleanup section turns alerts back on after the macro finishes or encounters an error.
Can Excel delete every worksheet in a workbook?
No. Excel requires at least one visible worksheet to remain. Ensure the keep list contains at least one worksheet, and do not attempt to delete the final remaining sheet.




