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.
by Allen Wyatt
(last updated September 26, 2020)
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, 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.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
One of the powerful programming structures available in VBA is the Select Case structure. This tip explains how you can ...Discover More
Excel allows you to create a special type of macro called a user-defined function (UDF). These can let you add to the ...Discover More
One way you can use macros in a workbook is to have them automatically triggered when certain events take place. Here's ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.