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.
Written by Allen Wyatt (last updated August 27, 2020)
This tip applies to Excel 97, 2000, 2002, and 2003
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!
If your macro processes information on a number of worksheets, chances are good that you need your macro to figure out ...Discover More
Macros are very powerful, but you may not want them to always be available to a user. Here are some ways you can limit ...Discover More
Macros are a great way to expand what you can do with the data you place in Excel. You can make those macros even handier ...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.