VBA & Macro Problems

How to Prevent Users from Deleting Excel Worksheets with VBA

Steven ParkerSteven Parker Nov 3, 2026 869 views

Question details

The user needs to stop collaborators from deleting Excel worksheets, including newly created ones, using VBA because standard workbook structure protection interferes with their necessary workflow.

How to Prevent Users from Deleting Excel Worksheets Using VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
Collaborating on a shared workbook where critical sheets must remain intact, but users still need enough freedom to perform tasks that standard structure protection normally blocks.
Observed behavior
Without protection, users can accidentally or intentionally delete essential worksheets. Standard protection blocks too many actions, necessitating a dynamic VBA-based workaround.
Before you start

Always test VBA macros in a blank copy of your workbook first to avoid unintended data loss, and remember that VBA solutions only function if the user enables macros upon opening the file.

Solution 1Recommended

Use ThisWorkbook Events to Toggle Protection

Deploy a combination of workbook event macros to automatically protect the workbook structure when users navigate away from a sheet, and delete any newly created unauthorized sheets.

Since Excel does not have a native Worksheet_BeforeDelete event, you can simulate this protection by leveraging the Workbook_SheetActivate and Workbook_SheetDeactivate events. This ensures the workbook structure is dynamically locked to prevent deletions when necessary.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to launch the Visual Basic for Applications (VBA) Editor.

2
Access ThisWorkbook Module

In the Project Explorer pane on the left side of the window, locate your active workbook and double-click on 'ThisWorkbook'.

3
Insert the VBA Code

Copy and paste the following code into the blank module window: Dim newSh As Boolean Private Sub Workbook_NewSheet(ByVal Sh As Object) Application.DisplayAlerts = False newSh = True Sh.Delete Application.DisplayAlerts = True End Sub Private Sub Workbook_SheetActivate(ByVal Sh As Object) Me.Unprotect End Sub Private Sub Workbook_SheetDeactivate(ByVal Sh As Object) If newSh Then newSh = False: Exit Sub Me.Protect End Sub

4
Save as Macro-Enabled

Go to File > Save As and ensure you save the file as an Excel Macro-Enabled Workbook (.xlsm) so the code functions properly.

Use ThisWorkbook Events to Toggle Protection
Macro Dependency: This method can be bypassed if users open the workbook with macros disabled. For strict security, standard structural protection is advised.
Advanced Spreadsheet Management

Protect Your Worksheets with WPS Spreadsheet

WPS Spreadsheet offers robust support for VBA macros alongside intuitive built-in workbook protection, allowing you to secure your data and prevent unwanted sheet deletions effortlessly.

  1. 1. Open Your Macro-Enabled File: Launch WPS Spreadsheet and open your existing .xlsm workbook.
  2. 2. Access the Developer Tools: Click on the 'Developer' tab on the ribbon and select 'VB Editor' to open the macro workspace.
  3. 3. Apply Security Macros: Double-click 'ThisWorkbook' in the Project pane, paste your sheet protection code, and save the document.
  4. 4. Use Native Protection: For a non-macro alternative, go to the 'Review' tab and click 'Protect Workbook' to instantly lock the file structure.
Highly compatible with Microsoft Excel (.xlsx and .xlsm) formats.Built-in Developer tools for writing and executing VBA scripts.Comprehensive structural protection features to lock crucial worksheets.Lightweight, fast, and features a familiar tabbed interface.
microsoft office alternative - wps office

Frequently Asked Questions

Can users bypass the VBA code to delete worksheets?

Yes. If a user chooses not to enable macros when opening the Excel file, or changes their Trust Center settings to block macros entirely, the VBA code will not execute. This leaves the worksheets unprotected and vulnerable to deletion.

Does Excel have a specific Worksheet_BeforeDelete event?

No, Excel does not feature a native Worksheet_BeforeDelete event. Developers typically rely on workarounds, such as using the Workbook_SheetDeactivate event to protect the workbook, or employing custom ribbon controls to intercept the delete action.

How do I stop users from deleting specific sheets but allow them to delete others?

You can modify your VBA event handlers to check the name of the active or deactivated sheet. By adding an 'If' statement that checks the sheet name (e.g., If Sh.Name = "CriticalData"), you can trigger workbook protection only when specific sheets are involved, leaving the rest editable.

Why use VBA instead of standard Workbook Protection?

Standard Workbook Protection locks the entire structure, meaning users cannot insert, move, hide, or unhide any sheets at all. VBA allows for more dynamic rules, enabling users to perform necessary daily tasks while strictly monitoring or preventing deletions in the background.