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

Removing Dashes from ISBN Numbers

ISBN numbers are used to denote a unique identifier for a published book. If you remove the dashes included in an ISBN, ...

Discover More

Table Numbers are Skipped

What do you do if you add numbered captions to an element of your document (such as tables) and Word skips a number? ...

Discover More

Finding Long Sentences

For certain types of writing, you may want to make sure that the sentences in your document do not exceed a certain ...

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)

Skipping Hidden Rows in a Macro

As your macro processes information in a worksheet, you may want to make sure that it skips over rows that are hidden. ...

Discover More

Adjusting Values with Formulas

Paste Special is a great tool that allows you to modify the values in a range of cells in your worksheets. You may want, ...

Discover More

Making a Cell's Contents Bold within a Macro

When your macro is processing information in a worksheet, do you need to periodically make the contents of a cell bold? ...

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