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

Finding and Replacing Text in Comments

Excel allows you to add comments to individual cells in your workbook. Unfortunately, Excel doesn't provide a way to search ...

Discover More

Displaying the Styles You Want Displayed

Want to customize your styles list so that it only lists the ones you are using? It's easy and can save some time when you ...

Discover More

Automatic Non-breaking Spaces in Dates

It drives some people crazy to have a date break across two lines. If you find yourself in this mindset, then you'll ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

MORE EXCELTIPS (RIBBON)

Selecting Visible Cells in a Macro

Many times you need to select just the visible cells before taking some action. It is helpful to know how to make this ...

Discover More

Adding a Calendar to a Worksheet

Using a specialized calendar control is a great way to let users add dates to a worksheet. Unfortuantely, Microsoft hasn't ...

Discover More

Friendly and Informative Error Handling

When creating macros, it is helpful to know what is going on within the macro itself in case an error crops up. Here's one ...

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 for this tip:

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

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.

Links and Sharing