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 April 13, 2024)
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.
Note:
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.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
When you have multiple workbooks open at the same time, Excel allows you to easily switch between those workbooks. How ...
Discover MoreWhen writing macros, you may want to position a message box at a specific location on the screen. This can't be done in ...
Discover MoreMany times you need to select just the visible cells before taking some action. It is helpful to know how to make this ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments