Вы находитесь на странице: 1из 5

Deploy your Excel macros from a central file

Have you ever wanted to use that really handy macro in all of your Excel worksheets? You can. This column explains how to place your macros in a file called personal.xls and make them available each time you start Excel. APPLIES TO Microsoft Excel 2002 By default, when you create a macro for a Microsoft Excel worksheet, that macro works only on that worksheet. This behavior is okay as long the macro automates tasks that are unique to that worksheet. But have you ever found yourself recreating the same macro on more than one worksheet? If you have (and even if you haven't), you can copy your macros to a worksheet called personal.xls. From there, your macros become available to you whenever you start Excel, and you can run them against the data on any open worksheet. Of course, you need to be careful here. You don't want to run macros at random, because doing so might corrupt your data and cost you time and money. But if you have macros that you can use in a variety of situations, using a personal.xls file can also save you time and money.

Getting started
To follow the steps in this column, you need an Excel worksheet that contains at least one macro. If you need a sample macro, follow these steps: 1. Start Excel and open a new, blank workbook. 2. On the Tools menu, point to Macro, and then click Visual Basic Editor. 3. In the Project window, double-click ThisWorkbook. 4. Copy and paste this code into the code window: sub test() MsgBox "This is only a test."
1

5. 6.

end sub Save the file to your default working folder. Quit the Visual Basic Editor and close the workbook.

NOTE. When you work with macros, you need to think about security. For more information about macro security, see Use Office security features to increase macro safety.

Create a personal.xls file


To create and use a personal.xls file, you follow this basic process: Use Microsoft Windows Explorer to create a blank personal.xls file in the folder called XLStart. If you installed Microsoft Office in the default location, you'll find \XLStart under C:\Program Files\Microsoft Office\Office 10. If you installed Office in a different location, find \XLStart there. Hide the file in Excel (Window menu, Hide command). Unhide the file (in the world of Excel, "unhide" is a real word), copy your macros from their parent worksheets to the file, and then hide personal.xls again. You keep personal.xls hidden unless you want to add macros to it. Whenever you start Excel, personal.xls opens automatically and runs in the background. If you don't hide the file, Excel opens it and allows you to add data to it. To create the personal.xls file 1. 2. Start Windows Explorer. Navigate to C:\Program Files\Microsoft Office\Office 10\XLStart. If you installed Excel or Microsoft Office in another location, navigate to the \XLStart folder in that location. 3. In the right pane of Windows Explorer, right-click, point to New, and then click Text Document. 4. By default, Windows Explorer allows you to rename the file. Rename it personal.xls. 5. If Windows asks you whether you want to change the file name extension, click Yes. To hide personal.xls 1. 2. 3. Start Excel and open personal.xls. On the Window menu in Excel, click Hide, and then quit Excel. When asked if you want to save your changes, click Yes. The workbook will open silently and become available the next time you start Excel.

Add macros to personal.xls


When you create a new macro, you can choose to save it to your personal.xls file. In addition, you can add your existing macros to personal.xls. The following steps explain how to perform both actions. To add new macros 1. On the Tools menu in Excel, point to Macro, and then click Record new macro. 2. In the Store macro in list, click Personal Macro Workbook.

3.

Click OK and record your macro normally.

To add existing macros 1. 2. 3. 4. 5. 6. 7. 8. 9. Open personal.xls, and on the Window menu, click Unhide. In the Unhide dialog box, ensure that personal.xls is selected, and then click OK. Open the worksheet that contains the macro you want to copy, and on the Tools menu, point to Macro, and then click Macros. Select the macro you want to add to personal.xls, and then click Edit. This step starts the Visual Basic Editor and opens the code module for the macro. On the File menu in the Visual Basic Editor, click Export File. Locate the \XLStart folder and click Save to save the code module in that folder. Go back to personal.xls, press ALT+F11 to start the Visual Basic Editor there, and on the File menu, click Import File. Navigate to the \XLStart folder, click the module that you just exported, and then click Open. Save the module, quit the Visual Basic Editor, and then hide personal.xls (Window menu, Hide command). Quit Excel, clicking Yes to save your changes.

10. One final task: Go back to the \XLStart folder and delete any code modules. If you leave the module files in the folder, they appear automatically the next time you start Excel. Use personal.xls You use personal.xls by starting Excel normally and running macros as needed. Open a new, blank worksheet, and use your favorite method to enter some data. 2. On the Tools menu, point to Macro, and then click Macros. 3. Select the macro you want to run, and then click Run. Okay, that's pretty basic, but keep these facts in mind as you go: Excel distinguishes between macros that reside in personal.xls and those that reside on the open worksheet. In the following figure, the Apply Sum macro resides on the local worksheet. The remaining macros reside on personal.xls: 1.

You can't edit macros that reside in personal.xls unless you open that file. For example, repeat steps 1 and 2 at the beginning of this section, select a macro that resides in personal.xls, and then click Edit. Excel displays the following message: Cannot edit a macro on a hidden workbook. Unhide the workbook using the Unhide command. In addition to containing macros, personal.xls can store information that you use frequently. For example, you can store an address or some boilerplate text in a cell and use a macro to copy that data and paste it onto another worksheet. Go ahead and use your creativity!

A few words of caution


Remember the caution at the beginning of this column? It's worth repeating: Don't run just any macro against any data. If you do, you can corrupt that data. In addition:
4

Get into the habit of checking your title bar when you open new or existing worksheets. If you've opened personal.xls, hide it, quit Excel, click Yes to save your changes, and start over. Make sure you use digitally signed macros that come from trusted sources, and implement a high level of macro security. For more information about digital signing, trusted sources, and macro security, see the following Power User columns:

Use Office security features to increase macro safety The Office security settings in depth Using Office security features with your default folders

Вам также может понравиться