Edit a macro

Edit a macro

Edit a macro

To edit a macro that is attached to a Microsoft Excel workbook, you use the Visual Basic Editor.
IMPORTANT: Before you can work with macros, you have to enable the Developer tab. For more information, see Show the Developer tab.

Change macro security settings

To edit and run macros, you must set the security level to temporarily enable all macros:
  1. On the Developer tab, in the Code group, click Macro Security.
    Code group on the Developer tab
  2. Under Macro Settings, click Enable all macros (not recommended, potentially dangerous code can run), and then click OK.
    WARNING: To help prevent potentially dangerous code from running, we recommend that you return to any of the settings that disable all macros after you finish working with macros.

Edit the macro

  1. On the Developer tab, in the Code group, click Macros.
  2. In the Macro name box, click the macro that you want to edit.
  3. Click Edit. The Visual Basic Editor appears.
TIP: To get help while you are working in the Visual Basic Editor, on the Help menu, click Microsoft Visual Basic Help, or press F1.


Copy a macro module to another workbook

If a workbook contains a Visual Basic for Applications (VBA) macro that you would like to use elsewhere, you can copy the module that contains that macro to another open workbook by using the Microsoft Visual Basic Editor.
Before you start working on macros you should:
  1. Enable the Developer tab before you work in macros. For more information, Show the Developer tab.
  2. Set the security level temporarily to enable all macros. To do that:
    1. On the Developer tab, in the Code group, click Macro Security.
    2. In the Macros Settings category, under Macro Settings, click Enable all macros (not recommended, potentially dangerous code can run), and then click OK.
      The Code group on the Developer tab
      IMPORTANT: To help prevent potentially dangerous code from running, we recommend that you return to any of the settings that disable all macros after you finish working with macros.

Copying a module from one workbook to another

  1. Open the workbook that contains the module that you want to copy and the workbook that you want to copy the module to.
  2. On the Developer tab, in the Code group, click Visual Basic.

    This starts the Visual Basic Editor.
  3. In the Visual Basic Editor, on the View menu, click Project Explorer Project Explorer button image , or press CTRL+R .
    That starts the Project task pane, and the pane lists both open workbooks. Typically, you'll see VBAProject (<workbook name>).
  4. In the Project pane, drag the module that you want to copy to the destination workbook. The module will be copied as Module1 to the Modules node underneath VBAProject (Book1.xlsm)
    VBA Project Explorer 
     
    Macro Module1 -- copied from Book2.xlsm to Book1.xlsm
    Copy of macro Module1

Overview of macros and VBA

If you are unfamiliar with macros and VBA in general, you may find the following information helpful.
  • A macro is an action or set of actions that you can use to automate tasks.
  • You can record macros by using the Record Macro command (Developer tab, Code group).
  • Macros are recorded in the VBA programming language.
  • You can inspect and edit your macros in the Visual Basic Editor, a window that is opened by Excel.

    A module that contains two macros stored in Module1 of Book1
Macros named MakeCellGreen and SetRowHeight are in a module named Module1, which is stored in Book1.

Comments