Display Alerts

It can be annoying to have to respond to system alerts while a macro runs. For example, if a macro deletes a worksheet, an alert message appears and you have to click the OK button to continue. However, there is also the possibility of a user clicking the Cancel button, which would abort the delete operation and could adversely affect subsequent code where the delete operation was assumed to have been carried out.

You can suppress most alerts by setting the DisplayAlerts property to False. When you suppress an alert dialog box, the action that is associated with the default button in that box is automatically carried out, as follows:

Application.DisplayAlerts = False ActiveSheet.Delete Application.DisplayAlerts = True

It is not necessary to reset DisplayAlerts to True at the end of your macro because VBA does this automatically. However, it is usually a good idea, after suppressing a particular message, to turn the alerts back on so that any unexpected warnings do appear on screen.

DisplayAlerts is commonly used to suppress the warning that you are about to overwrite an existing file using File O SaveAs. When you suppress this warning, the default action is taken and the file is overwritten without interrupting the macro.

0 0

Post a comment