How to Disable Alerts in Excel VBA (With Easy Steps)

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.

Overview image of excel vba disable alerts

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.

Dataset for excel vba disable alerts

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.

Open Visual Basic Editor

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.

Insert VBA Code

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

VBA code for excel vba disable alerts

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.

Code breakdown


📌 Step 3: Run the VBA Code

On this occasion, the VBA code removes the “Sheet4(Temp)” worksheet shown in the image below.

Running VBA Code to delete specific worksheet

  • Click the Run button or press the F5 key to run the macro as shown in the GIF below.

Running the macro and showing results

Finally, the “Temp” worksheet is removed as shown in the picture below.

Removed worksheet


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

How to Display Alerts in Excel VBA

  • 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.

Steps to enable alerts in VBA


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.

Things to Remember entering correct sheet name

  • Second, if the worksheet does not exist, then you’ll encounter an error message.

Error running VBA code

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.

Practice Section


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


<< Go Back to Alerts in Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Eshrak Kader
Eshrak Kader

Eshrak Kader is a dedicated professional with a BSc. Degree in Naval Architecture and Marine Engineering from Bangladesh University of Engineering and Technology. He boasts a rich background in Microsoft Office Suite, and over the past year, he has authored over 120 insightful articles for the ExcelDemy. Currently, Eshrak leads a team of 5 in the ExcelDemy Video project and enjoys problem-solving and making videos on Excel. Eshrak’s passion for continuous learning underscores his commitment to excellence in... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo