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

Changing Y-Axis Label Width

Excel provides quite a bit of flexibility in how your chart looks. One of the least flexible places is in adjusting the ...

Discover More

Adding Comments to Your Document

If you would like to add non-printing notes to your document, the Comments feature is one way of doing that. Here's how ...

Discover More

Converting Strings to Numbers

When creating macros, you often need to convert a text string that contains numbers into actual numeric values. You do ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Trouble Recording Paste Special Formula

Sometimes, when you upgrade to a new version of Excel, you could run into a problem recording macros that you had no ...

Discover More

Specifying Location for a Message Box

When writing macros, you may want to position a message box at a specific location on the screen. This can't be done in ...

Discover More

Deleting Zero Values from a Data Table

Want to get rid of all the zero values in a range of cells? This tip provides a couple of different ways you can ...

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 8 + 0?

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.