Create a macro

Important notice for users of Office 2003    To continue receiving security updates for Office, make sure you're running Office 2003 Service Pack 3 (SP3). The support for Office 2003 ends April 8, 2014. If you’re running Office 2003 after support ends, to receive all important security updates for Office, you need to upgrade to a later version such as Office 365 or Office 2013. For more information, see Support is ending for Office 2003.

ShowRecord a macro

  1. Set the security level to Medium or Low.

ShowHow?

  1. On the Tools menu, click 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.

  1. On the Tools menu, point to Macro, and then click Record New Macro.
  2. In the Macro name box, enter a name for the macro (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.). 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.
  1. If you want to run the macro by pressing a keyboard shortcut key (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.), 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 #.

 Note   The shortcut key will override any equivalent default Microsoft Excel shortcut keys while the workbook that contains the macro is open.

  1. In the Store macro in box, click the location where you want to store the macro.

If you want a macro to be available whenever you use Excel, select Personal Macro Workbook

  1. If you want to include a description of the macro, type it in the Description box.
  2. Click OK.
  3. 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 Button image so that it is selected. Excel will continue to record macros with relative references until you quit Excel or until you click Relative Reference Button image again, so that it is not selected.
  4. Carry out the actions you want to record.
  5. On the Stop Recording toolbar, click Stop Recording Button image.

ShowCreate a macro using Microsoft Visual Basic

  1. On the Tools menu in Microsoft Excel, point to Macro, and then click Visual Basic Editor.
  2. On the Insert menu, click Module.
  3. Type or copy your code into the code window of the module.
  4. If you want to run the macro (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.) from the module window, press F5.
  5. When you're finished writing your macro, click Close and Return to Microsoft Excel on the File menu.

ShowCreate a startup macro

ShowCopy part of a macro to create another macro

  1. Set the security level to Medium or Low.

ShowHow?

  1. On the Tools menu, click 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.

  1. Open the workbook that contains the macro (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.) you want to copy.
  2. On the Tools menu, point to Macro, and then click Macros.
  3. In the Macro name box, enter the name of the macro that you want to copy.
  4. Click Edit.
  5. Select the lines of the macro you want to copy.

To copy the entire macro, make sure to include the Sub and End Sub lines in the selection.

  1. On the Standard toolbar (toolbar: A bar with buttons and options that you can use to carry out commands. To display a toolbar, point to Toolbars on the View menu. If you don't see the button you want, click the arrows at the right end of the toolbar.), click Copy Button image.
  2. Switch to the module where you want to place the code.
  3. Click Paste Button image.

ShowTip

You can view your Personal Macro Workbook file (Personal.xls) at any time by opening it in the Visual Basic Editor (Visual Basic Editor: An environment in which you write new and edit existing Visual Basic for Applications code and procedures. The Visual Basic Editor contains a complete debugging toolset for finding syntax, run-time, and logic problems in your code.) (Alt+F11). Because Personal.xls is a hidden workbook that is always open, you must unhide it if you want to copy a macro.

 
 
Applies to:
Excel 2003