Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. 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: Running a Macro in a Number of Workbooks.
If you have quite a number of workbooks that you want to process through the use of macros, you may be tempted to place the processing macro within each workbook (as an Auto_Open macro), and then write some type of routine to load each workbook, in turn, and save it.
While this may sound good in theory, it won't work in practice. Why? Because when you open a workbook under macro control, the Auto_Open macro in the workbook being opened will not automatically run. There are three ways around this problem.
The first is to redo your macro so that you don't rely on Auto_Open macros in each workbook. If the Auto_Open macro in each workbook is the same, then why not simply move the code to a separate procedure in the controlling workbook? For instance, let's say you were using code that followed this process:
Sub MyMacro() Dim J As Integer Dim sTarget As String Application.ScreenUpdating = False For J = 1 To 999 sTarget = "Book" & Format(J, "000") & ".xls" Workbooks.Open sTarget 'Auto_Open runs here Workbooks(sTarget).Save Next J Application.ScreenUpdating = True End Sub
This won't work, for reasons already explained. One solution is to simply move the common Auto_Open code into another procedure, and then call it after opening the workbook, as shown here:
Sub MyMacro() Dim J As Integer Dim sTarget As String Application.ScreenUpdating = False For J = 1 To 999 sTarget = "Book" & Format(J, "000") & ".xls" Workbooks.Open sTarget Workbooks(sTarget).Activate DoCommonCode Workbooks(sTarget).Save Next J Application.ScreenUpdating = True End Sub
Sub DoCommonCode() 'Common code goes here End Sub
This approach works fine, provided the routine is the same that will be run on all your different workbooks. If the routines are different in each workbook, then you can force VBA to run the Auto_Open macro. This is done by using the RunAutoMacros method right after opening the workbooks:
Workbooks.Open sTarget ActiveWorkbook.RunAutoMacros xlAutoOpen
Given this approach, you could easily come up with a macro that would simply open each workbook (so the Auto_Open macros could run) and then save them. Such a macro would appear as follows:
Sub RunAutoOpenMacrosInBooks() Dim J As Integer Dim sTarget As String Application.ScreenUpdating = False For J = 1 To 999 sTarget = "Book" & Format(J, "000") & ".xls" On Error Resume Next Workbooks.Open sTarget Windows(sTarget).Activate With ActiveWorkbook If .Name <> ThisWorkbook.Name Then .RunAutoMacros xlAutoOpen .Save .Close End If End With Next i Application.ScreenUpdating = True End Sub
A third, and even better, approach is to not rely upon Auto_Open macros in each of your workbooks. Instead, rely on the Workbook_Open event as a way to run your macro. The Workbook_Open event is triggered automatically, regardless of whether the workbook is opened manually or in another macro. The code that the event contains is run automatically, just as you would expect of an Auto_Open macro.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (1850) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Running a Macro in a Number of Workbooks.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
How Excel uses templates is different than how Word uses templates. This tip looks at those differences and discusses ...
Discover MoreWhen you use macros to create functions, you might want to share those functions with others�"particularly if they ...
Discover MoreWant to know how much of a time difference there is between your machine and a different machine? This tip provides some ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2018-11-01 09:16:45
Willy Vanhaelen
This is a ridiculous tip. Why try to explain how to accomplish something using Auto_Open macros which is a relic of older Excel versions and maintained only for backward compatibility. At the end comes: "A third, and even better, approach is ... Instead rely on the Workbook_Open event ..." but no further explanation.
Why not start the tip with saying: "Using the Auto_Open macro is not an appropriate choice to solve this task. You can better do it by using the Workbook_Open event" and then continue to explain how you can accomplish this by using it?
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