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

Understanding Pattern Matching

Pattern matching is a type of searching you can do in Word that is very powerful. Despite its power, it remains rather ...

Discover More

Entering Dates in Excel

When you type information into a cell, Excel tries to figure out what type of information you are entering. If Excel can ...

Discover More

Deriving High and Low Non-Zero Values

When analyzing your numeric data, you may need to figure out the largest and smallest numbers in a set of values. If you ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Ctrl+Break Won't Work to Stop a Macro

When you need to stop a macro from running, you normally press Ctrl+Break. What are you to do if the keypress doesn't stop ...

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 Directory in a Macro

One of the things you can do with macros is to work with disk files. As you do so, you may have a need to create a new ...

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. 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 9 + 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.