Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Opening a Workbook but Disabling Macros.
by Allen Wyatt
(last updated August 27, 2020)
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 sown 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 (3158) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Opening a Workbook but Disabling Macros.
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!
Macros are really helpful, but you may want to actually delete a macro from within another macro. This is not as easy as ...Discover More
When creating macros, you can use the ColorIndex and Color properties to set or determine the colors in a cell. Excel ...Discover More
It is often helpful to get user input within a macro. Here's a quick way to present some options and get the user's response.Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.