Creating a *Macro in Excel 2003

Excel 2003 Microsoft Help Topic

About macros in Excel

If you perform a task repeatedly in Microsoft Excel, you can automate the task with a macro. A macro is a series of commands and functions that are stored in a Microsoft Visual Basic module and can be run whenever you need to perform the task. For example, if you often enter long text strings in cells, you can create a macro to format those cells so that the text wraps.

This topic provides an overview on recording and running macros in Excel. For more details, consult the actual Excel 2003 help file on this topic.

Definition: *macro: — An action or a set of actions that you can use to automate tasks. Macros are recorded in the Visual Basic for Applications programming language.

Recording macros Overview

When you record a macro, Excel stores information about each step you take as you perform a series of commands. You then run the macro to repeat, or "play back," the commands. If you make a mistake when you record the macro, corrections you make are also recorded.

Making a macro easy to run

You can run a macro by choosing it from a list in the Macro dialog box. To make a macro run whenever you click a particular button or press a particular key combination, you can assign the macro to a toolbar button, a keyboard shortcut (shortcut key: A function key or key combination, such as F5 or CTRL+A, that you use to carry out a menu command.

Macro security

Excel provides safeguards that help protect against viruses that can be transmitted by macros. If you share macros with others, you can certify them with a digital signature so that other users can verify that they are from a trustworthy source. Whenever you open a workbook that contains macros, you can verify their source before you enable them.

Before you create a macro - set the security level

  1. Set the security leve to Medium or Low.
    1. On the Tools menu, click Options.
      Macro Options
    2. Click the Security tab.
    3. Under Macro Security, click Macro Security.
    4. Click the Security Level tab, and then select the security level you want to use, either Medium or Low.
      Security Level
  2. On the Tools menu, point to Macro, and then click Record New Macro.
    Record New Macro
  3. In the Macro name box, enter a name for the macro
    Enter a name for the macro
    • Notes: The first character of the macro name must be a letter. Other characters can be letters, numbers, or underscore characters. Spaces are not allowed in a macro name; an underscore character works well as a word separator.
    • Do not use a macro name that is also a cell reference or you can get an error message that the macro name is not valid.
    • If you want to run the macro by pressing a keyboard**shortcut key, enter a letter in the Shortcut key box. You can use CTRL+ letter (for lowercase letters) or CTRL+SHIFT+ letter (for uppercase letters), where letter is any letter key on the keyboard. The shortcut key letter you use cannot be a number or special character such as @ or #.
    • **Shortcut key: A function key or key combination, such as F5 or CTRL+A, that you use to carry out a menu command. In contrast, an access key is a key combination, such as ALT+F, that moves the focus to a menu, command, or control.
    • Note: The shortcut key will override any equivalent default Microsoft Excel shortcut keys while the workbook that contains the macro is open.
  4. In the "Store macro in" box, click the location where you want to store the macro.
  5. If you want to include a description of the macro, type it in the Description box.
  6. Click OK.
  7. If you want the macro to run relative to the position of the active cell, record it using relative cell references. On the Stop Recording toolbar, click Relative Reference
    Relative Reference
    so that it is selected. Excel will continue to record macros with relative references until you quit Excel or until you click Relative Reference again, so that it is not selected.
  8. Carry out the actions you want to record.
  9. On the Stop Recording toolbar, click Stop Recording.
    Stop Recording

Run a Macro

When the macro security level in Excel is set to High, Excel will run only those macros that are digitally signed or stored in a trusted location, such as the Excel startup folder.

  1. If the macro you want to run is not digitally signed or located in a trusted location, you can temporarily set the security level to Medium or Low.
  2. How? — On the Tools menu, click Options. Click the Security tab. Under Macro Security, click Macro Security. Click the Security Level tab, and then select the security level you want to use.
  3. Open the workbook that contains the macro.
  4. On the Tools menu, point to Macro, and then click Macros.
  5. In the Macro name box, enter the name of the macro you want to run.
  6. Do one of the following: Click Run.
    Click Run.
  7. Or
    Start the macro from its designated keyboard shortcut.

Revised 04/Aug/2011

Back to the Home Page.
Back to the Home Page.