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: Develop Macros in Their Own Workbook.

Develop Macros in Their Own Workbook

Written by Allen Wyatt (last updated August 16, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


5

Excel includes VBA as a powerful programming language that you can use to develop all sorts of macros. It is not unusual, as you are developing macros, to go through many iterations and make wholesale changes to your macros. You may want to keep in mind, however, that doing so can cause problems in your workbooks.

As you make changes to macros, adding and removing code, the actual file used to store the macros (the workbook) can get quite fragmented. It seems that internally the macros are stored in blocks and, much like a disk drive, the blocks can become "non-contiguous" over time. (This happens only through editing, not through use of the macros themselves.) Some readers have reported that there are times the fragmentation can get so bad that the macros may fail or the workbook become unusable.

The solution to this potential problem is to do your macro development in a different workbook than the one that will eventually hold the macros. Thus, when the macro is transferred to its final home, it will be transferred as a contiguous block, rather than being fragmented.

If you want to make sure that the macro fragmentation is completely removed from a current workbook, all you need to do is export your VBA modules to text files, create a brand new workbook, and import the modules into it.

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 (10351) 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: Develop Macros in Their Own Workbook.

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

Calculated Dates

Word makes it easy to insert today's date in a document, but not as easy to insert a date X number of days in the future. ...

Discover More

Replacing Tildes at the Beginning of a Cell

Replacing a specific character (such as a tilde) seems a simple task, until you need to replace it only in a certain ...

Discover More

Wrapping Text in Merged Cells

When you are formatting your worksheet, Excel lets you easily merge adjacent cells together. If you want to wrap the text ...

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)

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

Negating a Cell Using a Macro

There are two ways to create macros: recording them or writing them from scratch. Some things cannot be done in a macro ...

Discover More

Checking if a Workbook is Already Open

Knowing if a workbook is already open can be a prerequisite to your macro working correctly. Here's how to check it out.

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-16 09:15:37

Brian

A slightly simpler way is to create a new module in the VBA editor, select the code then cut/paste it from the old module to the new one and then delete the old module. It's my understanding that when the new module is compiled it will be contiguous.


2021-02-11 08:08:31

mechie

In response to J Woolley - I have a few spreadsheets that are now over a decade old. The macros within them have continued to grow and have been edited periodically. Some of these sheets started with Excel ver 2003 as I recall (xlt). They have been .xltm workbooks every since we moved onto Excel 2007/2010. (Currently on ver 365.) I don't know if any of my updating over the years might have 'reset' the macros / vba, but I'm doubtful. I haven't noticed any issues as discussed in this tip. (As an aside, some of my macros got slower and slower over the years as we moved from one Windows version to the next, or one Excel version to the next. I had to retool some macros to make them more efficient. Eg - Using filter techniques instead of looping. But that is a whole other topic!)


2021-02-10 19:42:22

Peter

Thanks for the warning. I am an inveterate tinkerer.
I have the same question as mechie.


2021-02-09 13:19:48

J. Woolley

I believe the code fragmentation described in this Tip might have been a problem in older versions of Excel, but has anyone actually noticed this problem since Excel began using the Open XML (*.xlsm) workbook format?


2021-02-08 09:27:53

mechie

Can one export the module and then import it back into the workbook to clear VBA fragmentation?

My template workbooks have a LOT of other stuff going on (dozens of worksheets, hundreds of range names, lots of data, etc.) It is not just an empty workbook with macros. Perhaps if I were to export the VBA modules, strip all VBA (eg, save as an xlsx file), then turn it into an xltm file (new, fresh workbook) and import the VBA modules back in? Would that work?


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.