Please Note: This article is written for users of the following Microsoft Excel versions: 2007 and 2010. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Opening a Workbook but Disabling Macros.

Opening a Workbook but Disabling Macros

Written by Allen Wyatt (last updated March 27, 2023)
This tip applies to Excel 2007 and 2010


3

Bob is processing information in a workbook by using a macro. He would like for the macro to open a second workbook that has an AutoClose macro in it, but he doesn't want it to run when the second workbook is closed. He is looking for a way to open the second workbook, under the control of the macro in the first workbook, without enabling the macros in the second workbook.

There is no way to disable the macros in the second workbook when opening it under macro control. (If you are opening it manually, you can obviously hold down the Shift key as the workbook opens, but that doesn't help your macro—it has no fingers to hold down that key!)

There are a couple of workarounds, however. The first involves modifying your code that closes the second workbook, in this manner:

Application.EnableEvents = False
Workbooks("SecondBook.xls").Close
Application.EnableEvents = True

By setting the EnableEvents property to False, the event that is going to happen (closing the workbook) will not trigger the AutoClose macro. You can (and should) then set the EnableEvents property to True so that events can later continue.

Another workaround is to set some sort of "flag" in the AutoClose macro of the second workbook. This flag could test to see if the first workbook is open, and if it is, not run the main code in the AutoClose macro.

To do this, in the second workbook at the top of the module pages add the following code:

Dim AutoCloseDisabled as Boolean
Sub DisableAutoClose()
    AutoCloseDisabled=True
End Sub

Note that the declaration statement for the AutoCloseDisabled variable is outside of any procedure, which means that it will be global in scope and accessible within all the procedures.

Next, modify the AutoClose macro so that its body is enclosed within an If statement, as shown here:

Sub AutoClose()
    'variable declarations here

    If Not AutoCloseDisabled then

        'body of AutoClose here

    End if
End Sub

The idea is that when the second workbook is opened normally, the AutoCloseDisabled variable will be automatically set to False. (Boolean variables default to False when they are declared.) Since the DisableAutoClose procedure is never run in the workbook, the If statement in the AutoClose macro allows the actual body of the macro to be executed.

If you open the second workbook from your first workbook, then the code in your first workbook can call the DisableAutoClose macro in the second workbook, thereby setting the AutoCloseDisabled flag to True. This means that when the second workbook is closed, the If statement will skip over the body of the AutoClose macro.

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 (10232) applies to Microsoft Excel 2007 and 2010. You can find a version of this tip for the older menu interface of Excel here: Opening a Workbook but Disabling Macros.

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

Pulling Initial Letters from a String

When working with names or a different series of words, you may need to pull the initial letters from each word in the ...

Discover More

Two Printed Copies to Different Paper Trays

Many modern printers include multiple paper trays that can be used for different types or colors of paper. Word allows ...

Discover More

Displaying Worksheets in a Slideshow Fashion

Want to step through the worksheets in a workbook, displaying them like a slideshow? The macros provided in this tip can ...

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 (ribbon)

Inserting the Current Time with Seconds

If you need to insert the current time, with seconds, then you'll need the macro discussed in this tip. It's easy to use ...

Discover More

Macro Runs Slowly, but Steps Quickly

When you have a macro that processes a huge amount of data, it can seem like it takes forever to finish up. These ...

Discover More

Adding Differently Formatted Text to a Cell

Want to add or replace some text in a column with text that is formatted differently? The ideas presented in this tip can ...

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 2 + 8?

2018-04-20 18:46:49

Ken Kast

The comments say that there are problems with the tip. This tip has not been updated for over 4 years. The code still has the same problems. I find these tips valuable but what is the purpose of republishing something with identified errors. One shouldn't have to wade through old comments in order to determine if this is worth anything.


2014-02-14 09:28:29

Bryan

There's something amiss in this tip. For one, AutoClose will not automatically run when closing a workbook; you would instead need Auto_Close. If you really do have a macro called AutoClose, then it's being called by some other event (Workbook_BeforeClose, perhaps?)

Secondly, if you actually meant Auto_Close and not AutoClose, according to the help files (Workbook.Close Method): "Closing a workbook from Visual Basic doesn't run any Auto_Close macros in the workbook. Use the RunAutoMacros method to run the auto close macros." In other words, if you are using Auto_Close in the second workbook, you don't have to do anything special at all in the first workbook.

Thirdly, Auto_Close is not an "event", so setting EnableEvents to False wouldn't work, even if Auto_Close were being fired.

Taken in total, I'm guessing that Bob is *actually* using Workbook_BeforeClose to call an AutoClose proceedure. This is the only way to get the behavior described, and then to stop it by the steps Allen has described.

One little note: In your second code it'd be cleaner to use "If AutoCloseDisabled then Exit Sub" instead of wrapping the entire thing in an If statement.


2014-02-12 11:48:42

David Unger

Allen, if you're referring to "Auto_Close", it will run regardless of the EnableEvents setting (same for "Auto_Open"). However, your second workaround works fine.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu 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.