Delete Worksheets

Maintained on

VBA is a powerful tool for automating work in Excel.

Especially in worksheet operations, using VBA allows you to efficiently process data.

This article will explain in detail the procedure for deleting worksheets using specific code examples.

Basic Code for Deleting a Worksheet

Below is basic VBA code for deleting a worksheet.

Basic_code_to_delete_a_worksheet
Private Sub DeleteSheet()
  Sheets("Sheet1").Delete
End Sub

This code deletes the worksheet named “Sheet1”.

However, if you run this code as is, a confirmation message will be displayed during deletion, interrupting the code execution.

To avoid this, set Application.DisplayAlerts = False, which will prevent the confirmation message from being displayed during deletion. After deletion, re-enable warning messages with Application.DisplayAlerts = True.

Delete_worksheet_without_showing_message
Private Sub DeleteSheet()
    Application.DisplayAlerts = False
    Sheets("Sheet1").Delete
    Application.DisplayAlerts = True
End Sub
Warning

Since deleted worksheets cannot be restored, always check the contents before deletion.

Delete Only When a Specific Worksheet Exists

I’ll also introduce a method to check if a specific worksheet exists before deleting it.

Code_example_with_existence_check
Private Sub DeleteSheetIfExists(sheetName As String)
    Dim ws As Worksheet
    On Error Resume Next
    Set ws = Sheets(sheetName)
    On Error GoTo 0
    If Not ws Is Nothing Then
        Application.DisplayAlerts = False
        ws.Delete
        Application.DisplayAlerts = True
    Else
        MsgBox "Sheet " & sheetName & " does not exist."
    End If
End Sub

This code checks if a worksheet with the specified name exists and deletes it only if it does.

Deleting Multiple Worksheets at Once

There’s also a method for deleting multiple worksheets simultaneously.

Code_example_for_deleting_multiple_worksheets
Private Sub DeleteMultipleSheets(sheetNames As Variant)
    Dim sheetName As Variant
    Application.DisplayAlerts = False
    For Each sheetName In sheetNames
        On Error Resume Next
        Sheets(sheetName).Delete
        On Error GoTo 0
    Next sheetName
    Application.DisplayAlerts = True
End Sub

Before running this code, specify the worksheet names you want to delete as an array.

Execution_example_for_deleting_multiple_worksheets
Sub ExecuteDeleteMultipleSheets()
    Dim sheetsToDelete As Variant
    sheetsToDelete = Array("Sheet1", "Sheet2", "Sheet3")
    DeleteMultipleSheets sheetsToDelete
End Sub

Error Handling

It’s important to implement error handling in case an error occurs.

Code_example_with_error_handling
Sub DeleteSheetWithErrorHandling(sheetName As String)
    On Error GoTo ErrorHandler
    Application.DisplayAlerts = False
    Sheets(sheetName).Delete
    Application.DisplayAlerts = True
    Exit Sub
ErrorHandler:
    MsgBox "An error occurred while trying to delete the sheet: " & Err.Description
    Application.DisplayAlerts = True
End Sub
Importance of Error Handling

By implementing error handling, you can take appropriate action when unexpected errors occur and prevent the process from being interrupted.

Confirmation Message Before Deletion

Displaying a confirmation message before performing a delete operation can prevent accidental deletions.

Code_example_with_confirmation_message
Private Sub DeleteSheetWithConfirmation(sheetName As String)
    Dim ws As Worksheet
    On Error Resume Next
    Set ws = Sheets(sheetName)
    On Error GoTo 0
    If Not ws Is Nothing Then
        Dim confirmDelete As VbMsgBoxResult
        confirmDelete = MsgBox("Do you really want to delete " & sheetName & "?", vbYesNo + vbQuestion, "Delete Confirmation")
        If confirmDelete = vbYes Then
            Application.DisplayAlerts = False
            ws.Delete
            Application.DisplayAlerts = True
        End If
    Else
        MsgBox "Sheet " & sheetName & " does not exist."
    End If
End Sub

Troubleshooting Failed Worksheet Deletion

Possible causes for failed worksheet deletion include sheet protection, workbook protection, and shared workbooks. I’ll introduce solutions for each.

When_sheet_protection_is_the_cause
Private Sub UnprotectSheetAndDelete(sheetName As String)
    Dim ws As Worksheet
    On Error Resume Next
    Set ws = Sheets(sheetName)
    On Error GoTo 0
    If Not ws Is Nothing Then
        ws.Unprotect Password:="yourpassword"
        Application.DisplayAlerts = False
        ws.Delete
        Application.DisplayAlerts = True
    Else
        MsgBox "Sheet " & sheetName & " does not exist."
    End If
End Sub

When Workbook Protection Is the Cause

When the entire workbook is protected, use the following code to unprotect it.

Sub UnprotectWorkbookAndDelete(sheetName As String)
    ThisWorkbook.Unprotect Password:="yourpassword"
    DeleteSheetIfExists sheetName
    ThisWorkbook.Protect Password:="yourpassword"
End Sub

When a Shared Workbook Is the Cause

Sub UnshareWorkbookAndDelete(sheetName As String)
    If ThisWorkbook.MultiUserEditing Then
        ThisWorkbook.ExclusiveAccess
    End If
    DeleteSheetIfExists sheetName
End Sub

Quiz

Quiz

What happens when you run the following code?

Private Sub DeleteSheetIfCondition()
    Dim ws As Worksheet
    For Each ws In Worksheets
        If ws.Range("A1").Value = "削除" Then
            Application.DisplayAlerts = False
            ws.Delete
            Application.DisplayAlerts = True
        End If
    Next ws
End Sub
Quiz

How does the error handling in the following code work?

Private Sub DeleteSheetWithErrorHandling()
    On Error GoTo ErrorHandler
    Application.DisplayAlerts = False
    Worksheets("Sheet1").Delete
    Application.DisplayAlerts = True
    Exit Sub

ErrorHandler:
  MsgBox "シートの削除中にエラーが発生しました: " & Err.Description
End Sub

Summary

I’ve explained methods for deleting worksheets using VBA. Use these methods to further streamline your work in Excel.

#vba #Excel Operations #worksheet