Certainly, VBA Macros in Microsoft Excel help to solve various problems and automate repetitive tasks. Though alerts are intended to warn users of their actions or about errors, sometimes they can interrupt the execution of a macro. Now, this is especially infuriating when our macro takes a long time to run. Luckily, in the following tutorial, we’ll demonstrate the steps on how to disable alerts in Excel VBA. Moreover, we’ll also learn to enable alerts in VBA.
For instance, the above screenshot shows the alert prompted when deleting a worksheet. In the following sections, we’ll learn more about how to disable alerts in Excel VBA and the dataset with the necessary illustrations.
What Is VBA DisplayAlerts Property and How Does It Work?
First and foremost, let’s get ourselves up to speed with a quick explanation of what the Display Alerts property is in VBA.
Simply put, in VBA, the DisplayAlerts Application property takes a Boolean value that can be either True or False. Additionally, when the value is set to True, the DisplayAlerts property prompts warnings or messages during the execution of a macro to warn users of any potential errors. In contrast, setting the value to False suppresses all alerts.
Disable Alerts in Excel VBA: 3 Steps
In this case, let’s consider the Categorized Sales Dataset shown in the B4:D15 cells containing the “Category”, “Item”, and “Sales” in USD. Here, we want to disable alerts while deleting a worksheet in Excel, so just follow the steps shown below.
Here, we have used the Microsoft Excel 365 version; you may use any other version according to your convenience.
📌 Step 1: Open Visual Basic Editor
- First, go to the Developer tab >> click the Visual Basic button.
Not long after, this opens the Visual Basic Editor in a new window.
📌 Step 2: Insert VBA Code
- Go to the Insert tab >> select Module.
For your ease of reference, you can copy the code from here and paste it into the window as shown below.
Option Explicit
Sub Delete_worksheet()
Application.DisplayAlerts = False
Worksheets("Temp").Delete
Application.DisplayAlerts = True
MsgBox "Macro Executed Successfully!"
End Sub
⚡ Code Breakdown:
Now, in the following section, we’ll explain the VBA code used to disable alerts. In this case, the code is divided into 2 steps.
- In the first portion, the sub-routine is given a name, here it is Delete_worksheet().
- Next, set the Application.DisplayAlerts property to the Boolean value of False.
- Then, apply the Worksheets. Delete method to remove the specified worksheet.
- In the second portion, invoke the Application. Display Alerts property again, but this time, set it to True to make sure the code runs smoothly.
- Afterward, use the MsgBox function to prompt the message that the code ran successfully.
📌 Step 3: Run the VBA Code
On this occasion, the VBA code removes the “Sheet4(Temp)” worksheet shown in the image below.
- Click the Run button or press the F5 key to run the macro as shown in the GIF below.
Finally, the “Temp” worksheet is removed as shown in the picture below.
How to Enable Alerts in Excel VBA
Alternatively, we can enable the alerts in Excel VBA by changing the DisplayAlerts property Boolean value to True. Hence, let’s see it in action.
📌 Steps:
- At the very beginning, follow Steps 1-2 from the previous method to open the Visual Basic editor, insert a new Module and enter the code.
In this situation, the VBA code is almost exactly the same as before, 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
- Subsequently, hit the Run button >> to execute the Delete_worksheet_with_alert macro >> a warning pops out asking for the confirmation of the deletion >> press Delete to successfully execute the macro.
Things to Remember
As a note, here are a few key things to remember when applying the VBA code.
- In the first place, make sure to enter the name of the worksheet that you want to delete; in this case, it is the “Temp” worksheet.
- Second, if the worksheet does not exist, then you’ll encounter an error message.
Read More: How to Create Alerts in Excel
Practice Section
We have provided a Practice section on the right side of each sheet so you can practice yourself. Please make sure to do it by yourself.
Download Practice Workbook
Conclusion
To sum up, we hope this article helps you understand how to disable alerts in Excel VBA. Now, if you have any queries, please leave a comment below.
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!