VBA delete worksheet Excel Macro Example Code helps to delete Worksheet in Exce while automating some kind of requests. For example, We may have the request to collate the data from different worksheets of the workbook. We do automate it and finally we we delete the unnecessary worksheets from the workbook.
- Delete Worksheet in Excel VBA – An Example
- Delete Worksheet in Excel VBA – Disable Application Alerts
VBA Delete Worksheet in Excel – Solution(s):
We can delete a Worksheet from a Workbook using Delete Method of the Worksheet in Excel VBA. The following examples will show you how to delete a Worksheet form Workbook.
Delete Worksheet in Excel VBA – An Example:
In this example, I am deleting a worksheet (named Sheet2).
Code:
Sub sbDeleteASheet() Sheet1.Delete 'OR You can mention the Sheet name Sheets("Sheet2").Delete End Sub
Instructions:
- Open an excel workbook
- Insert 2 Worksheets and enter some data
- Press Alt+F11 to open VBA Editor
- Insert new module from the Insert Menu
- Copy the above code and Paste in the code window
- Save the file as macro enabled workbook
- Press F5 to execute the code.
Output:
When you execute the above code, it will show you a warning message called application alerts. If you click on delete button it will delete the Worksheet.
Delete Worksheet in Excel VBA – Disable Application Alerts or Warning message:
While executing the above code, we got a warning message. If you want to avoid that warning message, you need to disable the application alerts. The following example will show you how to delete a Worksheet and stop showing warning messages.
Code:
Sub sbDeleteASheet() 'Stopping Application Alerts Application.DisplayAlerts=FALSE Sheet1.Delete 'OR You can mention the Sheet name Sheets("Sheet2").Delete 'Enabling Application alerts once we are done with our task Application.DisplayAlerts=TRUE End Sub
Instructions:
- Open an excel workbook
- Insert 2 Worksheets and enter some data
- Press Alt+F11 to open VBA Editor
- Insert new module from the Insert Menu
- Copy the above code and Paste in the code window
- Save the file as macro enabled workbook
- Press F5 to execute the code.
Output:
If you execute the code, now you will not see any alerts. Since we have disabled the application alerts.
For the code above under ‘Delete Worksheet in Excel VBA – Disable Application Alerts or Warning message:’, the last line of code before end sub is ‘Application.DisplayAlerts=FALSE’. Shouldn’t this be ‘Application.DisplayAlerts=TRUE’?
Thanks Chris,
It should be Application.DisplayAlerts=TRUE, I changed it.
Thanks-PNRao!
I have a workbook with over 500 worksheets of which we only need 100. I know the names of the worksheets that need to be deleted but I am not sure where in the code I would add them. Your help is much appreciated! Thanks
Sub Deleting_Unrequired_worksheets()
Application.DisplayAlerts = False
Sheets(“Report Title”).Delete
Sheets(“Reject Details”).Delete
Sheets(“Query Details”).Delete
Sheets(“Manual Err Details”).Delete
Sheets(“Samplers Accuracy”).Delete
Sheets(“Samplers Details”).Delete
Sheets(“My Workflow”).Delete
Application.DisplayAlerts = True
End Sub
it’s good for me
thanks for sharing
and how to delete multiple sheets in vba ? help me please