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 August 14, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


8

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

Duplicating Drawing Objects

Need to duplicate a drawing object? It's easy to do if you use the same editing techniques you are already familiar with.

Discover More

Unwanted Page Breaks in Cross-References

You can use fields to cross-reference different text in a document. If you get unwanted results from the ...

Discover More

Converting from Relative to Absolute

Addresses used in a formula can be either relative or absolute. If you need to switch between the two types of ...

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)

Generating a Keyword Occurrence List

Need to pull a list of words from a range of cells? This tip shows how easy you can perform the task using a macro.

Discover More

Error Creating Event Handlers

If you are getting an error when you try to create an event handler, it could be related to a long-known bug in Excel. ...

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

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

2024-08-15 09:45:23

J. Woolley

@jamies
I was unaware of the semicolon (;) separator; perhaps you meant the colon (:) separator.


2024-08-14 07:36:31

Peter Johnson

James suggests “put multiple VBA commands on 1 line using the ‘ ; ‘ separator”. I agree this can be done especially in declarations.

However, I’d strongly advise against using this method. One of the aims of your VBA code should be that it’s well laid out and easily readable both for yourself and anyone who may have to maintain it later.

To that end best VBA practice is:
1. one statement per line
2. indent blocks
3. add comments to your code
4. leave blank lines in your code so that complete logical blocks stand out
5. write using subroutines that have well chosen (meaningful) names

Remember your goal should be code that is both reliable and maintainable.

There’s no benefit from either writing the longest possible macro (the stated question). Going to the other extreme and artificially shortening it by putting many statements on the same line is worse. It will confuse the reader (even if it doesn’t confuse VBA’s parser), make the code more difficult to test, debug and maintain.


2024-08-14 07:20:54

Peter Johnson

“A very long macro in Excel—over 1,400 lines”; really? That’s simply TOO long. It’s too long for any programming language or any application.

Skilled programmers write code in a well-structured way that does not ramble over many pages.

I’d be certain that recoding this monster 1,400 into smaller macros and making use of reusable code (instead of just replicating it) will make the code easier to read, easier to maintain and more reliable.


2024-08-14 05:32:24

jamies

Put multiple VBA commands on 1 line using the " ; " separator
But, ( from experience) there are some combinations that confuse the Syntax decoder.
So have a backup copy of the code and do the multi-line conversion in steps - making it easier to go back to undo a block of changes.

And when using VBA remember you should (usually) be using .VALUE2 rather than accepting the implied default of .VALUE


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.