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

Deleting Caption Labels

Define a label to be used in a caption, and you may later want to delete that label. Here's how you can easily make the ...

Discover More

Converting Inches to Points

Typographical measurements are often expressed in points. There are several formatting settings that, when accessed ...

Discover More

Creating a Floating Macro Button

Macros can make your use of Excel much more powerful. If you have a macro that is triggered by an on-screen button, you ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (menu)

Noting the Workbook Creation Date

You may want to add, to your worksheet, the date on which a particular workbook was created. Excel doesn't provide a way ...

Discover More

Using BIN2DEC In a Macro

Need a way, in a macro, to convert binary numbers into their decimal equivalents? There are two ways you can get the ...

Discover More

Counting Shaded Cells

Ever want to know how many cells in a worksheet (or a selection) are shaded in some way? You can create a handy little ...

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 seven more than 1?

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.