Excel VBA: Delete a Worksheet Without a Prompt Safely

VBA & macros · Updated November 3, 2026

Excel VBA: Delete a Worksheet Without a Prompt Safely

To delete an Excel worksheet without the confirmation prompt, set Application.DisplayAlerts to False immediately before calling the worksheet’s Delete method, then restore the setting. Excel will treat the deletion confirmation as accepted, so make a copy of the workbook first: deleting a sheet with VBA cannot be undone with Ctrl+Z.

Why Excel Shows a Deletion Prompt

Deleting a worksheet can permanently remove its cells, formulas, charts, tables, and other sheet content from the workbook. Excel normally displays a warning so that a user can cancel an accidental deletion. In VBA, the warning is controlled by the application-level DisplayAlerts property.

When Application.DisplayAlerts is set to False, Excel suppresses many alerts generated while the macro runs. For a worksheet deletion, Excel selects the default response to the confirmation rather than pausing for a click. That makes the macro run unattended, but it also means there is no last-minute confirmation. The property applies to the Excel application, not just to one worksheet, so it should be disabled only around the operation that needs it.

A Safe Macro for One Worksheet

This example deletes a sheet named Old Report from the workbook containing the macro. It saves the previous alert setting and restores it after success or failure, instead of assuming that alerts were originally on.

Sub DeleteOldReportWithoutPrompt()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim oldAlerts As Boolean
    Dim errorMessage As String

    Set wb = ThisWorkbook
    oldAlerts = Application.DisplayAlerts
    On Error GoTo HandleError

    Set ws = wb.Worksheets("Old Report")
    Application.DisplayAlerts = False
    ws.Delete
    GoTo Finish

HandleError:
    errorMessage = Err.Description

Finish:
    Application.DisplayAlerts = oldAlerts
    If Len(errorMessage) > 0 Then
        MsgBox "The worksheet was not deleted: " & errorMessage, vbExclamation
    End If
End Sub

ThisWorkbook means the workbook that contains this VBA code. That is usually safer than ActiveWorkbook, which refers to whichever workbook happens to be active when the macro runs. The error handler also restores the saved alert setting if the named sheet cannot be found or Excel raises an error during deletion. The message is there to make a failure visible; suppressing the confirmation does not mean the macro should hide errors.

How to Add and Run the Macro

  1. Save a backup copy of the workbook. If the macro must be kept in the file, save it as an Excel Macro-Enabled Workbook (.xlsm).
  2. In Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor. On a Mac, use Option+F11; depending on the keyboard, you may also need to hold Fn.
  3. In the editor, choose Insert > Module, paste the code into the module, and change Old Report to the exact worksheet tab name.
  4. Return to Excel and run the macro from the Visual Basic Editor or from Developer > Macros. If the Developer tab is not visible, enable it in Excel’s ribbon settings.

VBA macros run in the desktop Excel application, not in Excel for the web. The web app can open a macro-enabled workbook, but it cannot run VBA code. On a Mac, Excel supports VBA, although the editor interface and some keyboard shortcuts differ from Windows. Do not enable a macro from an untrusted file merely to remove a warning; follow your organization’s macro security policy.

Choose the Right Workbook and Worksheet

A correct deletion command can still remove the wrong sheet if the macro targets the wrong workbook or relies on the current selection. Referencing a worksheet by name, as the example does, is clearer than using ActiveSheet.Delete. The name must match the tab name exactly, including spaces and punctuation. A missing name causes an error, which the example reports.

Use ThisWorkbook when the code is stored in the workbook whose sheet should be deleted. If the macro is stored in Personal.xlsb or an add-in and is meant to act on the workbook currently in use, you may need ActiveWorkbook instead. In that case, check which workbook is active before running the macro. Avoid relying on a selected sheet: a user can change the selection without changing the code, making an active-sheet macro less predictable.

Also check whether the sheet name is used elsewhere. Formulas, charts, named ranges, queries, or other macros may depend on the sheet. Excel can remove the worksheet while leaving references that no longer resolve as intended. A successful deletion is not proof that every dependent feature still works.

Deleting Several Sheets in a Batch

For a planned batch deletion, decide exactly which sheets qualify before turning alerts off. A common approach is to compare each worksheet’s name against a fixed list or naming rule, while explicitly preserving a required sheet such as Home. Review the matching names first, then delete only those matches. This is safer than deleting every sheet except the active one, because the active sheet may not be the one that needs to remain.

If a loop removes worksheets from the Worksheets collection, iterate from the highest index down to the lowest. Removing an item changes the indexes of later items, so a forward loop can skip a sheet. Keep the same pattern as the single-sheet example: save the existing DisplayAlerts value, disable alerts only while deletions occur, and restore it through a cleanup path even if an error interrupts the loop. Test the selection logic on a copy before using it on important data.

Do not assume that a name pattern is harmless. For example, a rule that matches every tab beginning with Temp could also match a sheet with useful data if its name happens to share that prefix. For sensitive or recurring automation, log or display the names that will be deleted before executing the deletion step.

Safety, Limits, and Common Errors

The main risk is irreversible loss. Ctrl+Z does not reverse a VBA deletion, and closing without saving is not a reliable recovery plan once other work has occurred. Keep a versioned backup or use an approved file recovery system. Before deletion, consider exporting or copying any content that may need to be retained.

  • The sheet name is wrong: Confirm the worksheet tab name, including spaces. The code uses Worksheets, which refers to worksheet tabs rather than chart sheets.
  • The workbook structure is protected: Excel may block adding, moving, or deleting sheets while workbook structure protection is active. Remove protection only if you are authorized and know the password or approved process.
  • The target is the last visible worksheet: Excel requires a visible worksheet to remain available, so a macro should preserve a visible sheet or create a replacement before removing one.
  • Alerts remain disabled: If a macro exits early without restoring DisplayAlerts, later operations may run without expected warnings. Save and restore the prior value in a cleanup section, as in the example.
  • The code is blocked or will not run: Check the workbook file type, macro security settings, and whether you are using desktop Excel rather than Excel for the web.

Disabling alerts affects more than the worksheet confirmation if other workbook operations run during the same interval. Keep the interval short and avoid placing unrelated actions, such as saving over files or replacing existing files, inside it. The default response to a suppressed alert may not be appropriate for every operation.

When a Prompt Is Better Than Silent Deletion

Suppressing the built-in warning is useful for a routine, well-defined cleanup macro, such as removing a generated report tab that will be recreated. It is less suitable when the target is chosen by a user or when the workbook contains unique information. In those cases, leave Excel’s prompt enabled, or provide a deliberate confirmation of your own that names the exact sheet and workbook before deleting it.

Another alternative is to archive rather than delete. A macro can copy the sheet to a separate workbook, or a user can move it into an archive workbook, before removal. Hiding a sheet is another reversible option when the goal is simply to keep it out of view; it does not remove the data, and users with access to the workbook may be able to unhide it. Choose based on whether the requirement is to remove content, retain a recoverable record, or reduce clutter.

Frequently Asked Questions

What is the shortest VBA command to delete a sheet without asking?
Use Application.DisplayAlerts = False, call the target worksheet’s Delete method, and then turn alerts back on or restore the saved prior setting. For reliable code, use cleanup logic so the setting is restored even if deletion fails.

Does setting DisplayAlerts to False delete the sheet permanently?
It suppresses the confirmation and lets Excel proceed with the deletion; it does not create a backup. Treat the deletion as irreversible through Excel’s normal Undo command, and keep a copy if the sheet might be needed later.

Can I delete the active worksheet with VBA?
Yes, but targeting a worksheet by a known name is generally more predictable than using ActiveSheet. If you do use the active sheet, verify which workbook and tab are active before running the macro, and ensure another visible worksheet will remain.

Why does my macro still fail after I turn off alerts?
DisplayAlerts suppresses prompts; it does not override workbook structure protection, fix a misspelled sheet name, or make an invalid deletion possible. Check the error message and the workbook’s protection and sheet state.

Can Excel for the web run this VBA macro?
No. Excel for the web does not run VBA macros. Open the workbook in the desktop version of Excel to run VBA; if browser-based automation is required, investigate Office Scripts instead, which use a different programming model.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.