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

by Allen Wyatt
(last updated February 13, 2014)

2

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.

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

Understanding Smart Cut and Paste

Editing is generally made easier by a feature that Word calls smart cut and paste. If you prefer, you can turn the feature ...

Discover More

Anchoring Objects by Default

When you position objects (such as text boxes or graphics) on a page, one of the things you can do is to anchor the object so ...

Discover More

Checking for Missing Quotation Marks

Word provides handy spelling and grammar checkers. The grammar checker won't catch everything, however. One thing it won't ...

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)

Swapping Two Numbers

When programming macros, variables are used extensively. At some point you might want to exchange the values held by two ...

Discover More

Retrieving Drive Statistics

Need to gather some information about the drives on a system? It can be pretty easy to do using a macro, as shown in this ...

Discover More

Selecting a Specific Cell in a Macro

Need to use a macro to select a specific cell in a different workbook? It's not as straightforward of a proposition as you ...

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}] 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 nine more than 8?

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.