Maintenance Notification: We are upgrading our servers today so you may experience intermediate outages. Thank you for your patience.
Manage workbooks
MO-211

Copy macros between workbooks

  1. 1Open both workbooks: Macros.xlsm (has the macro) and Log.xlsm (needs it).
  2. 2Alt+F11 for the Visual Basic Editor; Ctrl+R if the Project pane is hidden.
  3. 3Drag the module Stamps from VBAProject (Macros.xlsm) onto VBAProject (Log.xlsm).
  4. 4Close the editor and save Log.xlsm as a macro-enabled .xlsm.

A macro lives in a module, so you copy the module. Dragging it onto the other workbook's project copies it and leaves the original where it was.

What the MO-211 task asks

Our MO-211 practice exams do not test this one yet: each download is a single workbook, and copying a macro needs a second one to copy from. We say so rather than guess at the wording of a real-exam task.

A task for it has to name both workbooks and the macro or module, for example: copy the StampDate macro from Macros.xlsm into Log.xlsm. Whatever the wording, the result that counts is the module inside the destination workbook, saved in a format that keeps it.

Running another workbook's macro is not the same thing. Alt+F8 lists the macros of every open workbook, so StampDate can run on Log while Macros.xlsm is open; once Macros.xlsm closes, Log has nothing.

Worked examples

Set up the grid below to follow along. Each example starts from it as shown; every result is what Excel showed.

Log.xlsm, sheet Log (the copied StampDate macro writes Checked into D1)
ABCD
1VesselBerthFee
2OspreyB490
3TernC2180
Drag the module across
Do this
  1. 1Open Macros.xlsm and Log.xlsm. Alt+F11.
  2. 2In the Project pane, drag Stamps (under Macros.xlsm › Modules) onto VBAProject (Log.xlsm).
  3. 3Back in Excel, on Log: Alt+F8, StampDate, Run.
Excel showsLog!D1: Checked · Macros.xlsm still has its Stamps module

The copy is a module of Log's own, with the same name and code. The source keeps its module.

The copy does not need the source
Do this
  1. 1After copying, close Macros.xlsm.
  2. 2On Log: Alt+F8, StampDate, Run.
Excel showsLog!D1: Checked

This is the test that you copied the macro rather than borrowed it: it still runs with the other workbook closed.

Export and import instead
Do this
  1. 1In the editor, right-click Stamps (Macros.xlsm) › Export File, and save Stamps.bas.
  2. 2Select VBAProject (Log.xlsm), then File › Import File, and pick Stamps.bas.
Excel showsLog.xlsm has a module named Stamps

The same result through a file, useful when the two workbooks cannot be open together.

Copied twice by mistake
Do this
  1. 1Import Stamps.bas into Log a second time.
  2. 2Alt+F8, StampDate, Run.
Excel showsModules Stamps and Stamps1 · the macro does not run

The second copy gets a new module name, but both hold a StampDate, and Excel will not run an ambiguous name. Remove the extra module.

In the video
Do this
  1. 1Alt+F11, drag Stamps onto VBAProject (copy-macros.xlsm).
  2. 2Close the editor; Alt+F8, StampDate, Run.
Excel showsD1: Checked

The first example, done in Excel with both workbooks open. In the video the Log workbook is saved as copy-macros.xlsm, so that is the project name you see.

Watch it done in Excel

The last example above, recorded in real Excel on the same grid.

Exam traps

Saving the destination as .xlsx

An .xlsx cannot hold macros. Saved as .xlsx, Log loses the copied module: reopened, it has no macros at all. Keep it .xlsm.

Borrowing instead of copying

Running 'Macros.xlsm'!StampDate from the Macros list while both files are open changes Log, but copies nothing. Check the module is listed under VBAProject (Log.xlsm).

Two copies of one macro

Dragging or importing twice adds Stamps1 beside Stamps, and StampDate then refuses to run. Remove the extra module.

FAQ

Does copying a module move it?

No. The source workbook keeps its module; the destination gets its own copy.

Can I copy just one macro out of a module?

Yes: copy its lines from Sub to End Sub in the code window and paste them into a module in the other workbook. Dragging copies the whole module.

Do the copied macros need the source workbook open?

No. Once copied, the macro runs with the source closed.

Try it under exam conditions

Take a full, timed MO-211 practice exam, upload the workbook you built, and get a score out of 1000 with a per-task breakdown of where the points went. Your first one is free.

No credit card needed.