Macros Run Fine Individually, but Not Collectively


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.

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!

More ExcelTips (ribbon)

Trimming Spaces from Strings

Need to get rid of extraneous spaces before or after the text in a string? VBA provides three different functions you can use ...

Discover More

Opening a Workbook and Suppressing Automatic Macros

Want to stop Excel from running any automatic macros that may be stored with a workbook? Here's how to do it.

Discover More

Determining the RGB Value of a Color

Excel allows you to fill a cell's background with just about any color you want. If you need to determine the RGB value of ...

Discover More

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.


If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is nine minus 5?

2013-11-12 09:18:28


I don't have a problem with the ActiveWorkbook and Selection objects. If your macros run against multiple worksheets or workbooks then you should always initialize those objects before using them and never assume their values.

2013-11-11 08:17:09


I think the only correct answer to this question is "show me your macros, the sheets you run them on, and what line is bringing the error".

There's really no way to tell why the problem is occurring from such a generic problem statement, though this is yet another argument against using the ActiveWorkbook and Selection objects.

2013-11-09 20:54:53

Chuck Trese

fyi, I can say from experience, Excel does properly wait for one macro to finish before it executes the next. A batch file is usually different because you are generally kicking off different (independent) applications that are designed to run in parallel. But since this is all within Excel, it behaves 100% linear. The only exception I'm aware of is if you launch a modeless form, you could get things out of order from what you intended.

2013-11-09 20:28:06

Chuck Trese

I have a Macro-macro, a "RunAll", of sorts. Aware that many of my macros do their work on the ActiveSheet, I preface each sub-macro with a Sheetx.Activate command. (In my case, the macros called are button-clicks on the respective sheets.)

Something like this:
Call Application.Run("'" & ThisWorkbook.Name & "'!" & "Sheet5.PTCViewShowTypeALL_Click")
Call Application.Run("'" & ThisWorkbook.Name & "'!" & "Sheet5.PTCViewGraphStandard_Click")
Call Application.Run("'" & ThisWorkbook.Name & "'!" & "Sheet6.PTCViewShowTypeALL_Click")
Call Application.Run("'" & ThisWorkbook.Name & "'!" & "Sheet6.PTCViewGraphStandard_Click")

It may not be the full solution to Guillerom's problem, but it has kept me out of trouble.

2013-11-09 18:55:10


If the individual macros are run in the order that they are specified in the RunAll macro and they run successfully, then I do not think this explanation is valid.

I do not know the rules for running macros within a macro, but is it possible that later macros are not waiting for the earlier ones to finish before starting? This could be tested by putting in a long time delay after each macro in the RunAll macro, perhaps by doing putting in a loop to execute an appropriate number of times before proceeding.

I know that in a batch file successive executions do not wait for the earlier ones to complete before starting.

This Site

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.

Newest Tips

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.