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

Reordering Last Name and First Name

If you've got a list of names in a column, you may want to change the order of each name. For instance, the name have the ...

Discover More

Running a Procedure when a Workbook is Opened

Ever want to have Excel run a procedure whenever you open a workbook? It's not as difficult as you might think. Here's how.

Discover More

Converting Tables to Text

Need to convert all the tables in your document into plain text? This tip provides a macro that can make quick work of a ...

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

Stepping Through a Non-Contiguous Range of Cells

Using macros to step through each cell in a selection is a common occurrence. What if that selected range is made up of ...

Discover More

Limiting Scroll Area

If you need to limit the cells that are accessible by the user of a worksheet, VBA can come to the rescue. This doesn't ...

Discover More

Copying Pictures with a Macro

Copying information using a macro is rather simple, although there are multiple ways you can do the copying. The most ...

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 6 + 5?

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.