Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 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

Written by Allen Wyatt (last updated September 26, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


4

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

Complex Compound Formatting

Sometimes it can seem that the formatting needs of your document can easily outstrip the capabilities of Word. This is ...

Discover More

Removing Pictures for a Worksheet in VBA

Excel allows you to add pictures to your worksheet, even within a macro. However, you might have a bit harder time ...

Discover More

Creating a Custom Power Management Plan

Don't like the built-in power management plans that Windows provides? You can create your own to better fit your power needs.

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)

Reversing Cell Contents

Macros are great at working with text. This tip presents an example that shows this versatility by reversing the contents ...

Discover More

Calling a Macro from the Workbook_Open Event

You can run a Personal.xlsb macro from within your Workbook_Open code, but you may get an error if you don't make sure ...

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}] (all 7 characters, in the sequence shown) 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 1 + 1?

2021-01-25 05:16:42

Gisel Avaleazar

I have a code, which is generated by a script, which is about 10000 lines. I can generate code for VBA, PHP, JavaScript, etc. It runs fine everywhere except Excel.. VBA no problem in LibreOffice for instance.


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.