Macros in Template Files

by Allen Wyatt
(last updated May 15, 2015)

3

Kay wrote to describe a problem she was having with a template, a macro, and a toolbar button. It seems that Kay created a macro, saved it in the workbook, and assigned the macro to a toolbar button. When she later saved the workbook as a template, the toolbar button no longer worked properly if she deleted the original workbook.

When you create a macro, you have the opportunity to specify exactly where it should be stored. If you store it in a workbook and then later save the workbook as a template, the macro is still there because the workbook is converted to a template that contains all the original macros stored with the workbook.

The problem is with the toolbar button. When you create a toolbar button and assign a macro to it, Excel remembers where the macro is stored. When the workbook was originally created, the macro was stored in the workbook. This means that the toolbar button "points" to the macro in the workbook. Even after the workbook is saved as a template, the toolbar button still points to the macro in the workbook, not in the template.

To correct this situation, all you need to do is—after you save the workbook as a template—make sure you open the template and reassign macros to the toolbar buttons. These macros should be ones that reside in the template itself, not in any other workbook you have open at the time. You can then save the template and everything should work fine. You can reassign the macros by following these steps:

  1. Open the new template file. (Make sure you open the actual XLT file, and that you don't create a new XLS file based on the template.)
  2. Right-click the toolbar button that runs the macro.
  3. Choose Customize from the resulting Context menu. Excel displays the Customize dialog box. (See Figure 1.)
  4. Figure 1. The Customize dialog box.

  5. Again right-click on the toolbar button that runs the macro.
  6. Choose Assign Macro from the resulting Context menu. Excel displays the Assign Macro dialog box.
  7. In the Macro Name box you will see the name of the macro assigned to the button. It should consist of a worksheet name (XLS) and the macro name, separated by an exclamation point. Change the worksheet name to the template name. (This may be as simple as changing the letters XLS to XLT.)
  8. Click on OK.
  9. Click on Close
  10. Resave your template.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2717) applies to Microsoft Excel 97, 2000, 2002, and 2003.

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

Removing Line Numbering

If you've added line numbers to your document, at some point you may want them turned off. Here's how to disable line ...

Discover More

Finding Cells Filled with a Particular Color

Do you need to find cells that are formatted with a particular color? How you accomplish this task depends on your version of ...

Discover More

Aligning Borders with the Page Margins

Add a border to a paragraph and you may find that it extends to the left and right of the regular text margins. To pull the ...

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 (MENU)

Item Not Available in Library

When sharing workbooks with others, you may find that the macros in those workbooks may not work as you expect. This tip ...

Discover More

Deleting a Macro

Don't need that old macro any more? Here's how to get rid of it so that it is no longer a part of your workbook.

Discover More

Working while a Macro is Running

If you have a macro that takes a long time to process a workbook, you might want to continue working in Excel while the macro ...

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 for this tip:

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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. 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 two more than 9?

2016-02-14 06:51:20

Willy Vanhaelen

@Rolf
These tips refer only to Excel 97-2003.
For Excel 2007 and up, refer to: http://excelribbon.tips.net/

.XLAM is not a template but an add-in (Excel 2007 and up).

Templates in Excel 2007 and up can be saved as .XLTX (no macros) or .XLTM with macros.


2016-02-13 11:40:15

Rolf

Well, this all worked fine for previous Excel versions. But as of Excel 2016, you can ONLY save with macros if the extension is .XLAM, whereas templates can only be named .xlsx (which can't contain macros.)

So, something like the template Personal.xl?? will not work -- unless you can find a trick. Please let me know if you have one!
Rolf


2013-04-11 08:40:48

jehad Zuqibeh

thanks


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.

Links and Sharing