Method 1 – Open Visual Basic Editor
- Go to the Developer tab >> click the Visual Basic button.
This opens the Visual Basic Editor in a new window.
Method 2 – Insert VBA Code
- Go to the Insert tab >> Select Module.
For ease of reference, copy the code from here and paste it into the window below.
Option Explicit
Sub Delete_worksheet()
Application.DisplayAlerts = False
Worksheets("Temp").Delete
Application.DisplayAlerts = True
MsgBox "Macro Executed Successfully!"
End Sub
⚡ Code Breakdown:
We’ll explain the VBA code used to disable alerts. The code is divided into 2 steps.
- The sub-routine is given a name; here, it is Delete_worksheet().
- Set the Application.DisplayAlerts property to the Boolean value of False.
- Apply the Worksheets. Delete method to remove the specified worksheet.
- Invoke the Application. Display Alerts property, but this time, set it to True to ensure the code runs smoothly.
- Use the MsgBox function to prompt the message that the code ran successfully.
Method 3 – Run the VBA Code
Tthe VBA code removes the “Sheet4(Temp)” worksheet in the image below.
- Click Run or press the F5 key to run the macro, as shown in the GIF below.
The “Temp” worksheet is removed.
How to Enable Alerts in Excel VBA
Enable the alerts in Excel VBA by changing the DisplayAlerts property Boolean value to True.
Steps:
- Follow Steps 1-2 from the previous method to open the Visual Basic editor, insert a new Module and enter the code.
The VBA code is almost the same, except for the Application.DisplayAlerts property, which has been set to True.
Option Explicit
Sub Delete_worksheet_with_alert()
Application.DisplayAlerts = True
Worksheets("Temp").Delete
MsgBox "Macro Executed Successfully!"
End Sub
- Hit Run >> to execute the Delete_worksheet_with_alert macro >> a warning pops out asking for the confirmation of the deletion >> Press Delete to execute the macro successfully.
Things to Remember
- Enter the name of the worksheet you want to delete, in this case, the “Temp” worksheet.
- If the worksheet does not exist, then you’ll encounter an error message.
Download Practice Workbook
Related Articles
- How to Create Popup Reminder in Excel
- How to Generate Automatic Email Alerts in Excel
- How to Create Notifications or Reminders in Excel
- How to Set Due Date Reminder Formula in Excel
- How to Set Due Date Reminder in Excel
<< Go Back to Alerts in Excel | Learn Excel
Get FREE Advanced Excel Exercises with Solutions!