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.
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:
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.
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!
You can create macros that run whenever Excel detects a certain event happening within an entire workbook. This tip ...
Discover MoreDo you have a macro that needs to read and write files? If so, then there is a good chance you need to specify the ...
Discover MoreWhen you use macros to create functions, you might want to share those functions with others�"particularly if they ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.
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.
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