Thursday, November 15, 2018

Copy a macro module to another workbook

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 Visual Basic Editor (VBE).

Macros and VBA tools can be found on the Developer tab, which is hidden by default, so the first step is to enable it.

  • For Windows, go to File > Options > Customize Ribbon.

  • For Mac, go to Excel > Preferences... > Ribbon & Toolbar.

  • Next, in the Customize the Ribbon section, under Main Tabs, check the Developer check box.

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 on the Developer tab.

  • 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. Here's an example of the VBE window for Windows:

    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.

Copying a module from one workbook to another

  1. Open both the workbook that contains the macro you want to copy, and the workbook where you want to copy it.

  2. On the Developer tab, click Visual Basic to open 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 .

  4. In the Project Explorer pane, drag the module containing the macro you want to copy to the destination workbook. In this case, we're copying Module1 from Book2.xlsm to Book1.xlsm.  

    VBA Project Explorer

  1. Module1 copied from Book2.xlsm

  2. Copy of Module1 copied to Book1.xlsm

Need more help?

You can always ask an expert in the Excel Tech Community, get support in the Answers community, or suggest a new feature or improvement on Excel User Voice.

No comments:

Post a Comment