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


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.


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


Maintaining Fields in a Merged Document

When merging documents, you may want to include some fields in the merged documents. For some fields this is impossible, ...

Discover More

Undoing an Edit

We all make mistakes. Fortunately, Excel makes it rather easy to undo your makes, right after you make them.

Discover More

Noting Inactivity within a Timeframe

There are many times when you are creating a worksheet that you need to analyze dates within that worksheet. Once such ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Selecting a Range of Cells Relative to the Current Cell

When processing information in a macro, you often need to select different cells relative to the currently selected ...

Discover More

Resetting Default Names for New Worksheets

When you add a new worksheet to a workbook, Excel gives it a default name that consists of "Sheet" followed by a number. ...

Discover More

Self-Aware Macros

Sometimes it may be helpful for a macro to know exactly where it is being executed. This tip provides a way that you can ...

Discover More

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.


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

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.