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.

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, 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

Auto Creation of an Acronym List

If you use a lot of acronyms in your documents, you may want a quick way to compile those acronyms and their definitions ...

Discover More

Counting Words the Old Fashioned Way

One way to specify word count is to count characters and divide by five. If you still need this old-fashioned way of ...

Discover More

Setting Page Margins

When getting ready to print your worksheet, you may want to take a moment to check what margins Excel will use on the ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Selectively Importing Records

Want to easily control which records get imported from a text file into Excel? It's easy to do when you write the macro ...

Discover More

Removing All Macros

Macros are stored as part of a workbook so that they are always available when you have the workbook open. If you want to ...

Discover More

Finding the Last-Used Cell in a Macro

Ever wonder what the macro-oriented equivalent of pressing Ctrl+End is? Here's the code and some caveats on using it.

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 9 - 5?

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.