Microsoft Excel 2003: Macros
Microsoft Excel 2003: Macros
Macros
OTS PUBLICATION : E16 • REVISED 12/4/2006 • TRAINING@ TOWSON . EDU • OFFICE OF TECHNOLOGY SERVICES
=Shortcut =Advice =Caution
About Macros
I’m sure you have used Excel enough to know that there are certain Macros
tasks that you seem to repeat over and over again. I’m sure you
1. About Macros
have thought, there must be a way to automate these repeated tasks
2. Set Up Excel to Allow
so that tasks can be repeated with minimal interaction by you. For
Macros
this, you need to create a Macro. A macro is a huge time saving
3. Open a Workbook that
feature that enables you record common or repetitive tasks and
Contains Macros
replay these tasks by “running” the macro when needed.
4. Run Macro
In Excel, rather than writing each step as code, you record a macro 5. Record a Macro
similar to recording a show on your VCR—except you are the show! 6. About Shortcut Keys
In other words, you start recording and Excel records each mouse 7. Modify a Macro
click and keystroke you make as you perform the task. When you are 8. About Visual Basic for
done, you stop recording. Voila! Your macro is ready and is now Applications (VBA)
available. Run the macro to repeat the same commands. Should you 9. Create a Custom
make a mistake, you can either re-record the macro or change it by Toolbar and a Custom
using the Visual Basic for Applications (VBA) Editor. Toolbar Button
10. Customize the Toolbar
Think through the steps that you want to record in a new macro. Macro Button
11. Create a Worksheet
Examples of Macros:
Macro Button
1. Insert a desired TU logo and set page setup. 12. Delete the Custom
Toolbar
2. Format column headings and data. 13. Delete a Macro
3. Sort data in multiple columns.
In the following exercise, you will set the Security level to Medium.
1. Open Excel.
2. Select Tools > Macro > Security from the menu bar.
© 2005 Towson University -This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs License.
Details available at http://www.towson.edu/OTStraining. 410-704-4070.
M ICROSOFT E XCEL 2003: M ACROS
Figure 1
If you are sure that the macros in the file are perfectly harmless (perhaps you created them yourself,
or the file came from someone you know) then click on “Enable Macros” (Figure 2,1). to open the
file and be able to use the macros within the workbook.
If you want to open the file but do not want the macros to be able to run, click on “Disable Mac-
ros” (Figure 2,2). This option will open the file in "Read Only" mode, which means you can view the
file, make changes and save it under a different name if you so chose.
Figure 2
2 1
2
M ICROSOFT E XCEL 2003: M ACROS
3. Select Tools > Macro > Macros from the menu bar.
4. In the Macro dialog box, select the desired macro (Figure 3, 1), and then click Run
(Figure 3, 2).
Figure 3
2
1
Record a Macro
The following demonstrates how to record a simple macro that inserts a TU logo located in the
Workshop Files folder on the Desktop. Once the logo is inserted, you will also record sizing the logo
to a proper size and place the logo in the upper right corner of the worksheet.
2. Select Tools > Macro > Record New Macro from the menu bar.
3. In the Macro Name field (Figure 4, 1), type a desired macro name, e.g. “Insert_Logo”.
Note: A macro name can be up to 255 characters. It must be unique and may not include any
spaces in the name.
Note: By default, the macro will be stored and available only in the current workbook; if appro-
priate, open the Store macro in drop-down list and select either New Workbook (to store and use
the macro only in a new empty workbook) or Personal Macro Workbook (to make the macro
available in all workbooks).
5. In the Description field (Figure 4, 3), type a desired description, e.g. “This macro inserts
the TU Logo”.
3
M ICROSOFT E XCEL 2003: M ACROS
7. Note: The word "Recording" will appear on the status bar to remind you that all keystrokes
and mouse actions are now being recorded. Depending on how your system is configured, a
Stop Recording toolbar may also appear in the window. If you make a mistake, simply cor-
rect it as you normally would and continue; both the mistake and its correction will become
part of the macro, and may be edited out later if desired.
8. Perform the necessary action to complete the macro, e.g. insert the TU logo from My Pictures
or a network location.
9. When ready, click the STOP button (Figure 5, 1) on the Stop Recording toolbar.
Note: if the toolbar is not visible, select Tools > Macro > Stop Recording.
Figure 4 Figure 5
1
2
1
3
If you have already recorded the macro and you can assign or change its shortcut key by doing the
following:
1. Select Tools > Macro > Macros from the menu bar.
2. In the Macro dialog box, select the name of the macro and click the Options button.
3. In the Shortcut Key field, type the desired shortcut key (Figure 6, 1) as indicated above.
4. When ready, click the OK button to close the Macro Options dialog box.
4
M ICROSOFT E XCEL 2003: M ACROS
Modify a Macro
Visual Basics for Applications (VBA) is the programming language used to create macros in the
Microsoft Office 2003 applications. When you record a macro, Excel translates the keystrokes and
mouse clicks into VBA code and creates and stores the macro for you.
Macros are grouped together in larger VBA code blocks called Modules. An Excel file can contain one
or more modules, and a module can contain one or more macros. Since a macro is a set of organ-
ized steps, a macro is also called a Procedure. All macro code can be viewed in the Code window
(Figure 7, 1). In the VBA, in order to see the macro code, you must first double-click the Modules
folder under the file name in the Project Explorer (Figure 7, 2). Each Module can have many macros/
procedures. Each macro/procedure begins with a “Sub” and end with an “End Sub” statement. All
text displayed in black is the actual macro code and any text displayed in green are comments and
will not be executed. To switch from the VBA back to Excel, you can either close the VBA window or
click the View Microsoft Excel button (Figure 7, 3)
Need Help? While in the VBA, press F1 on the keyboard to startup the Help Screen.
3 Figure 7
5
M ICROSOFT E XCEL 2003: M ACROS
4. In the New Toolbar dialog box, type “My Macros” (Figure 8, 1) and click OK (Figure 8, 2).
5. Click Close to close the Customize dialog box, or go on to the next section to add a custom
toolbar button.
Figure 8
1
2
4. Under Commands, select and drag the Custom Button icon (Figure 9, 1) onto any existing
toolbar or the new “My Macros” toolbar created in the previous section.
6. In the Assign Macro dialog box, select the desired macro (Figure 10, 1) and click OK
(Figure 10, 2).
Figure 9 Figure 10
2 2
6
M ICROSOFT E XCEL 2003: M ACROS
2. Select any desired image for the new macro button (Figure 11).
3. Right-click the new macro button again and select the text in the Name field, and type a desired
button name, e.g. “Insert TU Logo”.
5. Click the Close button in the Customize dialog box to close the dialog box.
Note: The macro can now be executed using the custom toolbar button.
Figure 11
2. Select View > Toolbars > Forms to display the Forms toolbar.
Figure 12
1
7
M ICROSOFT E XCEL 2003: M ACROS
2. Click and hold the mouse button and drag the pointer on the worksheet to indicate the location
and shape of the command button you wish to create.
3. Release the mouse button and the command button will be created and the Assign Macro dialog
box displays.
4. In the Assign Macro dialog box, select the desired macro (Figure 13, 1), and click the OK
(Figure 13, 2) button.
5. Select the text (“Button 1”) on the button face (Figure 14) and type a desired new button name.
6. Click away from the in the worksheet, but not on the command button, to de-select the com-
mand button.
Figure 13 Figure 14
3. Scroll to the bottom of the Toolbars section and select the desired toolbar.
2. Select Tools > Macro > Macros from the menu bar.
3. In the Macro dialog box, select the desired macro, and then click Delete.