Delete Excel Sheets with Specific Text After Name Using VBA
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.

- 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 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.
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.
Press Alt + F11 on your keyboard to open the Microsoft Visual Basic for Applications window.
Click 'Insert' in the top menu and select 'Module' to create a blank script window.
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
Press F5 or click the 'Run' button (the green play icon) on the toolbar to execute the macro and clean your workbook.

Use the InStr Function for Flexible Text Matching
If you want to delete sheets that contain the target string anywhere within the name (not just at the beginning), the InStr function is more appropriate.
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. Open Your Workbook: Launch WPS Spreadsheet and open the workbook containing the sheets you want to clean up.
- 2. Access the VBA Editor: Navigate to the 'Tools' or 'Developer' tab and click 'VBA Editor'.
- 3. Run Your Macro: Insert a new module, paste your worksheet deletion macro, and click Run.

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.




