Deleting Worksheets in a Macro

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:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2293) applies to Microsoft Excel 97, 2000, 2002, and 2003.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Space after a Table

Those familiar with styles are used to setting vertical spacing before or after paragraphs. You can get just the look you ...

Discover More

Last Document Saves Not Saved

Click the Save button and you expect your document to be saved, right? What if you later discover it wasn't really saved? ...

Discover More

Resetting Character Formatting in a Macro

Shortcut keys are a great way to apply styles to text in a document. You can easily create a shortcut key assignment for ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (menu)

Adding Leading Zeroes to ZIP Codes

Import a bunch of ZIP Codes into Excel, and you may be surprised that any leading zeroes disappear. Here's a handy little ...

Discover More

Easily Adding Blank Rows

Want to add a bunch of blank rows to a your data and have those rows interspersed among your existing rows? Here's a ...

Discover More

Relative VBA Selections

Need to select a cell using a macro? Need that selection to be relative to the cell you currently have selected? Here's ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 - 0?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.