Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. 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: Maximum Length Limit for a Macro.

Maximum Length Limit for a Macro

by Allen Wyatt
(last updated September 26, 2020)

3

Vasant has written a very long macro in Excel—over 1,400 lines. When he tries to run the macro, Excel refuses to run it and says that it is too long.

Excel apparently has a limit on VBA code such that you cannot have more than 64K of compiled code in a single procedure. The solution to this problem is to chop up your long macro into shorter procedures. For instance, you might divide your monster macro into, say, a dozen smaller macros. You can make the smaller macros Private instead of Public (so they don't show up in the Macros list in Excel), and then call them sequentially from a "controller" macro.

When you separate your code into individual procedures, make sure that each separate procedure has all loops and logic self-contained. Also make sure that any variables used in more than one procedure are declared as global variables so they are accessible by all the procedures.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10449) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Maximum Length Limit for a Macro.

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

Automatically Running a Macro

Word allows you to create macros that can run at special times, automatically. This tip explains five special macros that ...

Discover More

Enlarging Screen Font Size

Sometimes things appearing on the screen are a bit too small to read easily. One possible solution is to adjust the size ...

Discover More

Mirroring Documents

Have you ever wanted to have a Word document be accessible through two different folders? Here are several ways you can ...

Discover More

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)

Deleting Old Data from a Worksheet

If you keep on-going data in a worksheet, some of your data�"over time�"may need to be deleted. If you have an ...

Discover More

Using R1C1 Formula References in a Macro

Besides the regular way of displaying formulas, Excel can also display them using what is called R1C1 format. If you are ...

Discover More

Macros Run Slower in Newer Excel?

If you run a macro you used in an older version of Excel on a newer system, it may seem like the macro runs slower. Here ...

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 6Mpixels. 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 2 + 9?

2020-10-13 09:59:32

David Gray

Another excellent resource is Excel 2013: Power Programming with VBA, by John Walkenbach, ISBN 978-1-118-49039-6, especially chapters 6-9.


2020-10-11 11:25:43

David Gray

One significant advantage of coding your macros in the modular fashion described in this tip is that they are easier to debug for many reasons. Of these, the most significant is that you can divide and conquer by debugging each private routine separately. The easiest way to do this is by temporarily marking it as Public, then writing code that calls it in the Immediate Window. Another way to accomplish this is by writing a one-statement test routine, which lives in the module, and can be marked as Public as needed for testing, then marked as Private to hide it. Whether you call the test routine from the Immediate Window or put the cursor inside it and hit F5, the macro under test can call Debug.Print to write messages in the Immediate Window. These messages can either display intermediate values or be breadcrumbs that document the actual flow of execution.

Another significant advantage of modular coding is that each macro has fewer variables in scope. Not only is this fewer moving parts for you to monitor and manage, but it decreases the risk of accidentally changing a variable that was intended for the sole use of another part of the overall program.

Another side effect of breaking big code into modules is that you can reuse code. When the overall routine needs to do the same thing at several points in its process, it can call a subroutine, which can remain private. Moreover, by passing arguments (parameters) into the subroutine, it can use different data each time it is called.

Finally, breaking a big procedure into smaller chunks simplifies the code, making it easier to get your head around what the routine is doing.


2020-09-26 08:19:34

Alex Blakenburg

Whoa 1,400 lines in one Sub. The author of the macro might want to read Robert Martin's book Clean Code (he also has quite a few youtube videos)
I can't find an equivalent book using VBA in the examples but the principles are the same.


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.