by Allen Wyatt
(last updated June 3, 2016)
Guillermo has a series of macros that he routinely runs, and when he runs each of them manually, they work fine. He tried to place all of the macros into a "RunAll" macro (so he only has to run one macro instead of six individual macros) and now one of them doesn't run properly. When he executes the RunAll macro, he gets an error that a cell is protected and read-only. However, there is no protection on the sheet, and if he runs the macros individually, he doesn't get the error.
It should go without saying that the macro that executes fine individually but not in conjunction with the others is encountering something that is causing it to not work right. Likely that means that that a macro running before it is leaving the workbook in a different state than it would be in if you ran the macros individually.
For instance, if the misbehaving macro is the third in the series to run, check the first two macros to see what they are doing. Do they change the selected cells? Do they change the active worksheet? Do they change any conditions that the third macro expects? Any of these could cause the third macro to not function properly.
It is the second suggestion—about the active worksheet—that is the most likely scenario. When you run the macro individually, chances are good that you run it with a particular worksheet active. The other macros, though, may change the active worksheet to a different one, and that different worksheet may be the one that has protection applied to it.
The solution is to check each macro to make sure that it saves the locational information (worksheet, active cell, selected cells, etc.) before making any changes to any of those. With them saved, it is an easy matter to restore them at the end of the macro.
If that still doesn't do it, then you may need to do some debugging. In the "RunAll" macro, use the VBA Editor to establish a breakpoint on the line that calls the failing macro. You can then use the "Step Into" command to step into the macro and examine where the failure occurs. Pay particular attention to the what worksheet and cells the macro is trying to change, and then see if you can figure out what is going on with the earlier macros to lead to that worksheet and cells. You can use either of the following lines in the Immediate window to see where you are:
? ActiveSheet.Name ? ActiveCell.Address
While tracking down these types of errors can be a challenge, the light at the end of the tunnel is that you will end up with a perfectly functioning "RunAll" macro that can save you a great deal of time.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12710) applies to Microsoft Excel 2007, 2010, and 2013.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
If you need to know whether a particular value is odd or even, you can use this simple formula. Designed to be used in a ...Discover More
When working with data in a macro, there are two broad categories you can manipulate: numbers and text. Sometimes you ...Discover More
Open up a workbook, and Excel normally runs the macros associated with that workbook. You can disable the automatic ...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 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.