VBA & Macro Problems

Delete Excel Sheets with Specific Text After Name Using VBA

Jason MillerJason Miller Nov 3, 2026 872 views

Question details

The user needs to use an Excel VBA macro to delete worksheets whose names begin with a specific string (like "Company Status") and contain additional text, without deleting the sheet that matches the base string exactly.

How to Delete Excel Sheets with Text After a Specific Name Using VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
Cleaning up a workbook by removing generated or duplicate sheets that share a common prefix, while keeping the main template or summary sheet intact.
Observed behavior
Need a VBA script that safely evaluates sheet names, excludes the exact base name, and deletes the matching variations without triggering unwanted prompts.
Before you start

Before running any VBA macro that deletes worksheets, ensure you have saved a backup copy of your workbook. Worksheets deleted via VBA cannot be recovered using the standard Undo feature.

Solution 1Recommended

Use the VBA Left Function to Exclude the Exact Name

This method checks the beginning of each sheet name against your target string while explicitly telling the macro to skip the exact base sheet.

By utilizing the Left function, you can identify sheets that start with your desired phrase. We combine this with an inequality operator (<>) to ensure the main sheet is completely ignored by the deletion command.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Microsoft Visual Basic for Applications window.

2
Insert a New Module

Click 'Insert' in the top menu and select 'Module' to create a blank script window.

3
Paste the VBA Code

Enter the following script: Sub DeletePrefixedSheets() Dim ws As Worksheet Application.DisplayAlerts = False For Each ws In ThisWorkbook.Worksheets If ws.Name <> "Company Status" And Left(ws.Name, 14) = "Company Status" Then ws.Delete End If Next ws Application.DisplayAlerts = True End Sub

4
Run the Macro

Press F5 or click the 'Run' button (the green play icon) on the toolbar to execute the macro and clean your workbook.

Use the VBA Left Function to Exclude the Exact Name
Alert Suppression: The commands Application.DisplayAlerts = False and True ensure that Excel does not pop up a confirmation window for every single sheet it deletes.
WPS Spreadsheet Macro Support

Run VBA Macros to Manage Sheets in WPS Office

WPS Office fully supports VBA macros, allowing you to run your Excel scripts to delete, rename, or manage worksheets seamlessly. You can automate repetitive tasks just as you would in Microsoft Excel.

  1. 1. Open Your Workbook: Launch WPS Spreadsheet and open the workbook containing the sheets you want to clean up.
  2. 2. Access the VBA Editor: Navigate to the 'Tools' or 'Developer' tab and click 'VBA Editor'.
  3. 3. Run Your Macro: Insert a new module, paste your worksheet deletion macro, and click Run.
Fully compatible with Microsoft Excel VBA scripts and .xlsm formatsLightweight, fast execution for complex workbook operationsFree to use with advanced spreadsheet modeling toolsFamiliar user interface requiring zero learning curve
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get a warning message when the macro deletes a sheet?

Excel automatically prompts for confirmation before permanently deleting a sheet to prevent data loss. To suppress this warning, add the line 'Application.DisplayAlerts = False' before the deletion command, and set it back to 'True' right before the macro ends.

Can I undo a worksheet deletion made by a VBA macro?

No, actions performed by VBA macros clear the Undo stack in Excel. This means you cannot press Ctrl+Z to recover deleted sheets. Always save a backup of your workbook before running deletion scripts.

How do I modify the macro for a different sheet base name?

You need to update the string "Company Status" in the code to your new base name. If using the Left() function method, you must also adjust the number in the Left(ws.Name, 14) function to match the exact character count of your new base name.

Will this macro work if my sheet name has different capitalizations?

The standard equality operator (=) is case-sensitive. To make it case-insensitive, you can use the UCase() or LCase() functions to standardize the case before comparing, or use the InStr function with the vbTextCompare argument.