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

Inserting Initials and Date in a Comment

When you insert a comment into a document, Word keeps track of who entered it and the date when it was entered. Here's ...

Discover More

Including a Printer's Name in a Footer

If you can produce output on a number of different printers, you may want Word to indicate on your printouts which ...

Discover More

Referencing Fields in Another Document

Sometimes you may have two documents that are so integrally related to each other that the one document may require the ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling 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

Converting Numbers Into Words

Write out a check and you need to include the digits for the amount of the check and the value of the check written out ...

Discover More

Using Seek In a Macro

When reading information from a text file, your macro may need to start reading at a place other than the beginning of ...

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 five more than 3?

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.