Create a macro

Microsoft Office Excel 2003

  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.
  2. On the Tools menu, point to Macro, and then click Record New Macro.
  3. In the Macro name box, 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.

  4. 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 #.

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

  5. 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.

  6. If you want to include a description of the macro, type it in the Description box.

  7. Click OK.
  8. 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.

  9. Carry out the actions you want to record.
  10. 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 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

Automatic macros, such as Auto_Activate, are designed to run when you start Microsoft Excel. For more information about these macros, see Visual Basic Help.

ShowCopy part of a macro to create another macro

  1. Set the security level to Medium or Low.

    Show How?

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

  7. On the Standard toolbar, click Copy Button image.
  8. Switch to the module where you want to place the code.
  9. 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 (Alt+F11). Because Personal.xls is a hidden workbook that is always open, you must unhide it if you want to copy a macro.