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.

In this article

Overview of macros and VBA

Copying a module from one workbook to another

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 (create) macros by using the Record Macro command, located on the Developer tab in the Code group.

  • Macros are recorded in the Microsoft Visual Basic for Applications (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

In this example, macros named MakeCellGreen and SetRowHeight are in a module named Module1, which is stored in Book1.

Copying a module from one workbook to another

  1. If the Developer tab is not available, do the following to display it:

    1. Click the File tab, click Options, and then click the Customize Ribbon category.

    2. Under Main Tabs, select the Developer check box and then click OK.

  2. To set the security level temporarily to enable all macros, do the following:

    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.

      Code group on the Developer tab in Excel

      Note:  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.

  3. Open the workbook that contains the module that you want to copy and the workbook that you want to copy the module to.

  4. On the Developer tab, in the Code group, click Visual Basic.

    This starts the Visual Basic Editor.

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

  6. 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 folder underneath VBAProject (workbook name.xlsm).

    VBA Project Explorer

    Macro Module1. To copy it, drag from Book2.xlsm to Book1.xlsm.

    Copy of macro Module1, located under the Modules folder.

Top of Page

Share Facebook Facebook Twitter Twitter Email Email

Was this information helpful?

Great! Any other feedback?

How can we improve it?

Thank you for your feedback!

×