VBA & Macro Problems

Delete Excel Worksheets Except Selected Sheets with VBA

Tauseeq MagsiTauseeq Magsi Nov 12, 2026 869 views

Question details

Delete every worksheet in a workbook except the sheet names listed in a table.

Delete Excel Worksheets Except Selected Sheets with VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Create the worksheet keep list

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.

2
Open the Visual Basic Editor

In Excel, press Alt+F11, then select Insert > Module from the Visual Basic Editor menu.

3
Paste the VBA macro

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

4
Run the macro

Place the cursor inside DeleteUnlistedSheets and press F5, or return to Excel and choose Developer > Macros > DeleteUnlistedSheets > Run.

Delete unlisted worksheets by reading the keep-list table
Deletion is permanent: DisplayAlerts is disabled so Excel does not show the usual worksheet deletion confirmation. Verify the table entries before running the macro.
WPS Spreadsheet VBA tools

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. 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. 2. Confirm the keep-list entries: Open HIDDEN_DEV_SHEET and verify that the TableDontDel table contains every worksheet name that should remain.
  3. 3. Open the macro tools: Go to the Developer tab and select Visual Basic or Macro to access the workbook's macro project.
  4. 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. 5. Run the worksheet cleanup macro: Use Developer > Macros, select the macro, and choose Run after confirming the list of worksheets to keep.
Use a worksheet table to manage the sheet names that must be retained.Automate repetitive worksheet cleanup with macro tools.Open and edit Microsoft Excel .xlsx and macro-enabled .xlsm workbook formats.Work in a lightweight interface with familiar spreadsheet commands.
microsoft office alternative - wps office

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.