Written by Allen Wyatt (last updated May 15, 2021)
This tip applies to Excel 97, 2000, 2002, and 2003
Most Excel commands are available for use within your macros, provided you know the proper VBA commands to accomplish the task at hand. You can use the following macro command to delete the active worksheet:
ActiveSheet.Delete
If you issue the command in your macro, you will find that Excel pauses the macro and asks you if you are sure you want to delete the worksheet. When you click on Yes, the worksheet is deleted and the macro resumes.
The whole idea behind macros, of course, is to automate many of the tasks you do on a regular basis. Stopping and asking for confirmation may be the safe way to go, but it doesn't do much to help the cause of automation. If you want the worksheet to be deleted without a pause, there are a couple of things you can do. First, you can use the SendKeys method to simulate pressing the Enter key, which is the same as clicking on Yes in the confirmation dialog box. All you need to do is add a single line before the line that deletes the worksheet:
Application.SendKeys ("{ENTER}") ActiveSheet.Delete
SendKeys does nothing but stuff keypresses into the keyboard buffer, the same as if you typed them from the keyboard. Thus, the SendKeys line must precede the Delete line so that the Enter keypress is in the buffer before it is needed.
Any longtime macro developer can point to several potential problems with using SendKeys, the primary problem being that you cannot use it to specify that you are accepting the Yes option in the confirmation dialog box, and only in that dialog box. However unlikely, if some other dialog box pops up (perhaps one generated by a different program) at just the right time, the Enter keypress will be applied to that dialog box, not to the one you expected.
A better solution is to turn off the alerting capabilities of Excel for a short time. Consider the following macro code:
Application.DisplayAlerts = False ActiveSheet.Delete Application.DisplayAlerts = True
This code turns off the alerts, deletes the worksheet, and then turns the alerts back on. While they are turned off, Excel will not display the confirmation dialog box, but will act as if it had been displayed and the default option (Yes) selected.
It is important to remember the last line of code shown here. If you do not set the DisplayAlerts property back to True, then Excel will not show any more alert messages, even after the macro has ended. This could cause problems, as you might imagine. It is best to only set it to False for the short time you need the alerts turned off.
Even with DisplayAlerts set to False, you will still see error messages, if one is generated. For instance, if you execute the above code and there is only a single worksheet in the workbook, you will still see an error message. (This happens because you cannot delete the last worksheet in a workbook.)
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2293) applies to Microsoft Excel 97, 2000, 2002, and 2003.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
Grab some info from a source other than Excel, and you may find the need to delete a certain pattern of rows from a ...
Discover MoreYou may want to add, to your worksheet, the date on which a particular workbook was created. Excel doesn't provide a way ...
Discover MoreExcel allows you to define names that can refer to either ranges of cells or to constant information, such as formulas. ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments