|
Your VBA/Automation Headquarters!
|
Please excuse us while we construct this page!
What is a MacroMacros are used to create a set of commands or tasks to be performed automatically. When you have repetitive tasks, you can create a macro that will automatically run a small "program" to perform those tasks for you. Visual Basic for Applications (VBA) is a mini-version of Visual Basic that is used within the Microsoft Office suite and certain other programs, like Visio. When you record a macro, the VBA code is recorded for you. Many tasks that you would like to perform cannot be recorded, but must be written in VBA; and many tasks that you would like to perform all at once might need to be written by a VBA programmer. Recording macros in Word and Excel and other programs becomes easy once you know how.
Excel VBAA very important piece of information about Excel VBA: When you want a macro to be available for your use in ANY Excel file, it must be stored in the Personal.xls file. Read about it in your help files. TrainingNow you can learn Excel VBA at your own pace! Simply purchase these downloadable courses when you're ready. How to Record a Macro in ExcelThe following is step-by-step instructions to record a macro, make the macro available for use in any single file or for use in any file you open, and assign a toolbar button to run the macro. One of the best reasons for creating a macro could be that you must format files from external sources on a daily basis. If you are fairly accomplished in Excel, you will know that there are several steps you must take to accomplish it. The next time you need to perform this or a similar task, record the tasks as a macro and you will only have to press a button to perform them again. Step 1: Prepare to RecordIf you would like the macro to be available in a specific file, open that file. From the menu, choose Tools-Macro-Record New Macro. The following dialog box will appear:
In the Macro name box, enter the name you would like to use for your macro. It must not have any spaces and should not contain special characters. Decide where to store the macro. If you would like it available in the current workbook only, hit the Store Macro in dropdown box and select your active workbook's name. If you would like the macro to be available for use with any file, select Personal Macro Workbook (which is a file that will be called Personal.xls). Give the macro any description you like in the Description box. By default, the description will state the date the macro was recorded, and the user name. The user name automatically comes from the information under Word's Tools-Options, User Info tab. The Stop Recording toolbar will appear. There are two buttons on the Stop Recording toolbar. The square is used to STOP recording and the lines and circle are used to PAUSE recording.
NOTE: If you accidentally close the Stop Recording toolbar, go to View-Toolbars and click on the Stop Recording toolbar. If you accidentally use the X on the Stop Recording toolbar to stop recording a macro, go to View-Toolbars and click on the Stop Recording toolbar. Step 2: Record the MacroPerform the tasks you would like your new macro to perform for you. If you need to interrupt the macro recording to perform some non-related work, press the Pause button on the Stop Recording toolbar. Press the Pause button again to continue recording. Hit the Stop button to Stop recording when you have finished recording your tasks. Step 3: Test the MacroAlways test a macro, particularly if you have created it to run for someone else. To test the macro, hit Tools-Macro-Macros, and double-click the macro name. Step 4: Assign the Macro to a Toolbar ButtonHit Tools-Customize and choose the Commands tab. The following dialog box appears:
Under the Categories (left), scroll down and choose Macros. Click on the smiley icon and drag it up to any location you choose on any toolbar you choose. Your mouse pointer must have a plus sign (+) hanging on it before you lift your finger from the mouse or the icon will not be placed. While dragging, you will see an X hanging on your mouse pointer. Once your icon is placed on a toolbar, click the Modify Selection button, and various options will appear. The following describes only those options that apply for our purposes here: Name: Type in the name of the macro as it should appear on your toolbar if you are going to use the text style button (described below). Reset Button Image: Select this only if you have begun to edit an existing button image and you would like to reset it to the default. Edit Button Image: Select this option to "draw" your own button image. Change Button Image: Select this option to change the existing button image to one of your choosing from Office's collection of button images. Default Style: Clicking this button makes the button on the toolbar show just the icon and no text. Text Only (always): Clicking this button makes the button on the toolbar show just the text as it is shown in the Name box, which you can change. Image and Text: Clicking this button makes the button on the toolbar show both the icon and the text. When the button appears exactly as you would like it to appear on the toolbar, hit the close button on the Customize dialog box. The first time you hit your new toolbar button, you are asked which macro you want it to run. You'll pick your new macro from the dialog that pops up.
Word VBAHow to Record a Macro in WordThe following is step-by-step instructions to record a macro, make the macro available for use in any single file or for use in any file you open, and assign a toolbar button to run the macro. One of the best reasons for creating a macro could be that you must convert WordPerfect documents into Word documents. If you are fairly accomplished in Word, you will know that there are several steps you must take to accomplish it. The next time you need to perform this or a similar task, record the tasks as a macro and you will only have to press a button to perform them again. Step 1: Prepare to RecordIf you would like the macro to be available in a specific file, open that file. If you would like the macro to be available in all files, create a new, blank document. From the menu, choose Tools-Macro-Record New Macro. The following dialog box will appear:
In the Macro name box, enter the name you would like to use for your macro. It must not have any spaces and should not contain special characters. Decide where to store the macro. If you would like it available in the current document only, hit the Store Macro in dropdown box and select your active document's name. If you would like the macro to be available for use with any file, select All Documents (Normal.dot). Give the macro any description you like in the Description box. By default, the description will state the date the macro was recorded, and the user name. The user name automatically comes from the information under Word's Tools-Options, User Info tab. Step 2: Assign the Macro to a Toolbar ButtonClick on Tools-Customize and click on the Commands tab. The following dialog box appears:
The Commands tab will be selected. In the right column, you will see an icon with a title similar to your macro's name. Click on the icon and drag it up to any location you choose on any toolbar you choose. Your mouse pointer must have a plus sign (+) hanging on it before you lift your finger from the mouse or the icon will not be placed. While dragging, you will see an X hanging on your mouse pointer. Once your icon is placed on a toolbar, click the Modify Selection button, and various options will appear. The following describes only those options that apply for our purposes here: Name: Type in the name of the macro as it should appear on your toolbar if you are going to use the text style button (described below). Reset Button Image: Select this only if you have begun to edit an existing button image and you would like to reset it to the default. Edit Button Image: Select this option to "draw" your own button image. Change Button Image: Select this option to change the existing button image to one of your choosing from Office's collection of button images. Default Style: Clicking this button makes the button on the toolbar show just the icon and no text. Text Only (always): Clicking this button makes the button on the toolbar show just the text as it is shown in the Name box, which you can change. Image and Text: Clicking this button makes the button on the toolbar show both the icon and the text. When the button appears exactly as you would like it to appear on the toolbar, hit the close button on the Customize dialog box. The Stop Recording toolbar will appear. There are two buttons on the Stop Recording toolbar. The square is used to STOP recording and the lines and circle are used to PAUSE recording.
NOTE: If you accidentally close the Stop Recording toolbar, go to View-Toolbars and put a checkmark in the Stop Recording toolbar checkbox. If you accidentally use the X on the Stop Recording toolbar to stop recording a macro, go to View-Toolbars and put a checkmark in the Stop Recording toolbar checkbox and then use the appropriate button. Step 3: Record the MacroPerform the tasks you would like your new macro to perform for you. If you need to interrupt the macro recording to perform some non-related work, press the Pause button on the Stop Recording toolbar. Press the Pause button again to continue recording. Hit the Stop button to Stop recording when you have finished recording your tasks. Step 4: Test the MacroAlways test a macro, particularly if you have created it to run for someone else. To test the macro, click on the toolbar button you've created, or hit Tools-Macro-Macros, and double-click the macro name. |
|
Document_Open | |
|
Document_Close | |
|
Document_Save |
To see more events, just open the VBE window (Alt-F11), hit Help-Microsoft Visual Basic Help, then type in "event" without the quotes and hit enter.
![]()
![]()
Copyright© 2003 - 2004 The Office Experts
Microsoft Office®,
Access®, Excel®, FrontPage®, Outlook®, PowerPoint®,
Visio®, and Word®, are registered trademarks of the Microsoft Corporation