Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. 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 August 30, 2014)

5

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.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10449) applies to Microsoft Excel 2007, 2010, and 2013. 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

Determining Your Version of Excel

Want to find out exactly what version of Excel you are using? Here's how to get to the info.

Discover More

Differences in Behavior of Links

Got some active links in your document? Do you want to have them activated when you click on them, or do you want to require ...

Discover More

Changing Font Face and Size Conditionally

Conditional formatting does not allow you to change the typeface and font size used in a cell. You can write your own macro ...

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)

Delimited Text-to-Columns in a Macro

The Text-to-Columns tool is an extremely powerful feature that allows you to divide data in a variety of ways. Excel even ...

Discover More

Relative References when Recording Macros

When you record a macro, make sure that you know how Excel is recording your cell movements. This tip explains the problem ...

Discover More

Adding Buttons to Your Worksheet

You can easily add a button to your worksheet that will allow you to run various macros. This tip shows how easy it is.

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 four less than 4?

2014-09-02 11:03:13

Don

balthamossa...I think it means it loads and executes on a subroutine by subroutine basis. Since I keep functions grouped by purpose, e.g. those that return Worksheets, in modules separate from the Subs related to a specific task, it could not be working on a Module-level.

Tony....fully agree. I started my IT life trying to decipher un-modularized code. It's a nightmare.

Ken...carrying on with Tony's point, a 64k limit may not be necessary from a technical standpoint but it does discourage run-on subprocedures and their inherent problems. Way back when, the guideline was that each unit of code should have one purpose. This wasn't because of memory limits. It makes the code more testable, maintainable and re-usable by call instead of copy & paste.

In the case of a SUB or FUNCTION that is "too long" (by whatever measure one wishes to use, if there are a x lines in the Sub to make a decision, why not move that section to a Function, making it a testable, maintainable, and possibly re-usable-by-call unit of code?

I guess my point is that simply because a limitation can be removed is it always best to remove it? What is the value of a single Sub that is 128K long versus the same code split into Functions and Subs that might total 160K (adding a generous factor for new procedural headers, ENDs, and call statements). Disk an memory are cheap, labor to troubleshoot and repair are not.

Just my opinions...I guess it was pent up from a 3 day weekend.


2014-09-01 04:21:44

balthamossa2b

So does this mean Office stores all subs individually, not by modules?

...Huh.


2014-09-01 03:09:51

Tony

As a programmer by trade, I wouldn't have a macro of 1,400 lines; I prefer to break my code down for two reasons.
(1) move duplicate code into another macro (just like a subroutine) so that it's easier to maintain.
(2) If code is still large, break chunks of logic into another macro so that the main code is easier to read (and maintain). For example, if you're importing data into excel from a csv file with different record types, you could have a macro per record type.


2014-08-30 18:43:06

Ken

I have run up against the size limit but I didn't know exactly what it is, nor do I know know to find out the compiled size of a macro anyway. It is, however, completely absurd and should deeply embarrass Microsoft - 64k limits were obsolete when the world moved from DOS to Windows and have no place in one of the company's flagship products. Is ther any likelihood of them fixing this problem?


2014-08-30 11:46:39

Scott L

That's interesting. I knew there was a limit, but not what it was and I do break up my routines into subroutines. What about this scenario? I created an addin that contains all the frequently used macros I’ve collected over the years. It modifies the ribbon, adding new groups to every tab and a new tab that has buttons for all the macros, including a half dozen dynamic menus. I’ve found that some macros that run in other workbooks won’t run when the addin is loaded. If I unload it, they run fine, but not if it’s loaded. I must be running up against some other limit and I’m wondering what that is and how to deal with it.


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.