Macros Run Fine Individually, but Not Collectively

by Allen Wyatt
(last updated June 3, 2016)

5

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.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Using Duplex Printing

Want to print on both sides of a piece of paper? Some printers have the capability to do two-sided printing automatically. ...

Discover More

Changing Error Checking Rules

Excel can check the data and formulas in your worksheet to see if it detects any errors. The rules used for this checking can ...

Discover More

Applying Bold Italics

Applying bold and italics formatting to text is easy in Word. If you want to apply bold and italics simultaneously, you can ...

Discover More

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!

More ExcelTips (ribbon)

Replacing Some Formulas with the Formula Results

Macros are often used to process the data stored in a worksheet. Some of these processing needs can be pretty specific to ...

Discover More

Setting Column Width in a Macro

Does your macro need to change the width of some columns in a worksheet? Here's how to do it.

Discover More

Creating a String in a Macro

Need to put together a bunch of characters to create a text string? You can do it in your macros by using the String ...

Discover More
Subscribe

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

View most recent newsletter.

Comments

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. Maximum image size is 8Mpixels. 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 6 - 3?

2013-11-12 09:18:28

Jeff

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

Bryan

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:
'No_CC_Chart:
Sheet5.Activate
Call Application.Run("'" & ThisWorkbook.Name & "'!" & "Sheet5.PTCViewShowTypeALL_Click")
Call Application.Run("'" & ThisWorkbook.Name & "'!" & "Sheet5.PTCViewGraphStandard_Click")
'Ratings_Chart:
Sheet6.Activate
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

WarwickW

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
Subscribe

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.