bottom
Great ExcelTips!
         
Your e-mail address is safe!
Close Note

Tips.Net > ExcelTips Home > Macros > Macros in Template Files

Macros in Template Files

Summary: People often place macros in template files to perform any number of tasks. This tip describes a situation where the link between a toolbar button and a macro was broken when a new document was created from a template. The problem was corrected by double-checking where the toolbar button pointed when referencing the macro. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)

Subscriber Kay Rock 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. (Click here to see a related figure.)
  4. Again right-click on the toolbar button that runs the macro.
  5. Choose Assign Macro from the resulting Context menu. Excel displays the Assign Macro dialog box.
  6. 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.)
  7. Click on OK.
  8. Click on Close
  9. Resave your template.

Tip #2717 applies to Microsoft Excel versions: 97 | 2000 | 2002 | 2003


More Power! Expand your skills and make Excel really sing! It's all possible with macros. The best resource anywhere for macros is ExcelTips: The Macros. Check it out today!

Helpful Links

Ask an Excel Question
Make a Comment

Tips.Net Home
Vital News Home

ExcelTips FAQ
ExcelTips Premium

Learn Access Now

Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips

Advertise on the
ExcelTips Site

 

Great Info!

Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your e-mail address and click "Subscribe."
     
(Your e-mail address will never be shared with anyone, ever.)