Excel VBA: How to Copy a Module to Another Workbook

VBA & macros · Updated November 3, 2026

Excel VBA: How to Copy a Module to Another Workbook

To copy an Excel VBA module to another workbook, open both files in desktop Excel, press Alt+F11, and copy the module in the Visual Basic Editor from the source project to the destination project. You can also export the standard module as a .bas file and import it into the other workbook; for repeatable transfers, VBA can automate that process through the VBProject object model.

What copying a VBA module transfers

A VBA module is a container for code, such as macros, functions, and supporting procedures. Copying a module transfers that code container; it does not copy the worksheet, workbook data, buttons, or other objects that the code might use. This distinction matters because a copied procedure can appear in the destination yet fail when it expects a sheet, named range, form, or other resource that was not transferred.

The instructions below are for Microsoft Excel for Microsoft 365 on Windows. A standard module normally appears in the Visual Basic Editor under a workbook’s Modules folder. It often has a name such as Module1, though you can rename it in the Properties window. If the module contains code that refers to other modules or workbook-specific items, those dependencies need to be available in the destination too.

Excel for the web cannot open or edit VBA projects. Use the desktop application to copy or run VBA. On a Mac, the desktop Visual Basic Editor supports working with VBA, but some project-security settings and interface details differ; the automated Windows example below should not be assumed to work unchanged on every Mac setup.

Copy a module manually in the Visual Basic Editor

For a one-time transfer, copying and pasting the module is usually the simplest approach. Keep both workbooks open, and make sure you can identify the source and destination projects by their workbook names in Project Explorer.

  1. In Excel, open the source workbook and the destination workbook. Save a backup copy of the destination before changing its code.
  2. Press Alt+F11 on Windows to open the Visual Basic Editor. If Project Explorer is not visible, press Ctrl+R.
  3. In Project Explorer, expand the source workbook and its Modules folder. Select the module to copy.
  4. Choose Edit > Copy, or press Ctrl+C. Select the destination workbook’s project in Project Explorer, then choose Edit > Paste or press Ctrl+V.
  5. Check that the module appears under the destination project. Open it and review the code, then save the destination in a macro-enabled format.

The Mac Visual Basic Editor has the same general project-and-module model, although menu labels and keyboard shortcuts can vary with the Excel release and keyboard settings. Use the editor’s Edit menu if the Windows shortcuts do not match. Copying in the editor is useful when you want to inspect the code as you move it, but it can be less convenient for a large batch of modules or a transfer that must be repeated reliably.

Export a .bas file and import it

Exporting makes a portable file that can be stored, emailed through an approved channel, or imported into another workbook later. In the Visual Basic Editor, right-click the standard module in Project Explorer and choose Export File. Choose a location and save the module as a .bas file. In the destination project, right-click the project name or its Modules folder and choose Import File, then select that file.

Export and import are often preferable when you want to keep a reusable copy outside either workbook. A standard module is typically exported as .bas. Other component types have different file behavior: class modules use .cls, and a UserForm export can include both a .frm file and a companion .frx file containing form resources. Do not assume that exporting a standard module also exports forms, class modules, or workbook objects that the code calls.

Before importing, check whether the destination already has a component with the same module name. Excel may reject a duplicate or give the imported component a changed name, depending on the situation. A clear module name helps avoid confusion, but renaming code components can affect procedures or other code that refer to them by name.

Automate the transfer with VBA

If you need to transfer a standard module repeatedly, you can export it to a temporary .bas file and import that file into another workbook. The following example assumes the macro is stored in the source workbook, the destination workbook is already open, and the module named Module1 exists. Change the two constants to match your workbook and module names. It stops if the destination has a module with the same name rather than silently replacing code.

Save a copy of both workbooks before running any macro. VBA changes cannot be undone with Ctrl+Z. The routine also requires permission to access the VBA project object model, described in the next section.

Option Explicit

Public Sub CopyStandardModule()
    Const MODULE_NAME As String = "Module1"
    Const DESTINATION_NAME As String = "Report.xlsm"

    Dim sourceBook As Workbook
    Dim destinationBook As Workbook
    Dim openBook As Workbook
    Dim sourceProject As Object
    Dim destinationProject As Object
    Dim component As Object
    Dim importedComponent As Object
    Dim tempFile As String
    Dim errorText As String

    On Error GoTo Failed
    Set sourceBook = ThisWorkbook

    ' Find the already-open destination by its workbook name.
    For Each openBook In Application.Workbooks
        If StrComp(openBook.Name, DESTINATION_NAME, vbTextCompare) = 0 Then
            Set destinationBook = openBook
            Exit For
        End If
    Next openBook

    If destinationBook Is Nothing Then
        Err.Raise vbObjectError + 1000, , "Open the destination workbook first."
    End If
    If destinationBook Is sourceBook Then
        Err.Raise vbObjectError + 1001, , "Source and destination must differ."
    End If

    Set sourceProject = sourceBook.VBProject
    Set destinationProject = destinationBook.VBProject

    ' Do not import a second component with the same name.
    For Each component In destinationProject.VBComponents
        If StrComp(component.Name, MODULE_NAME, vbTextCompare) = 0 Then
            Err.Raise vbObjectError + 1002, , "The destination already has this module."
        End If
    Next component

    tempFile = Environ$("TEMP") & Application.PathSeparator & MODULE_NAME & ".bas"
    sourceProject.VBComponents(MODULE_NAME).Export tempFile
    Set importedComponent = destinationProject.VBComponents.Import(tempFile)
    Kill tempFile

    MsgBox "Module copied to " & destinationBook.Name & ".", vbInformation
    Exit Sub

Failed:
    errorText = Err.Description
    On Error Resume Next
    If Len(tempFile) > 0 Then
        If Len(Dir$(tempFile)) > 0 Then Kill tempFile
    End If
    On Error GoTo 0
    MsgBox "Copy failed: " & errorText, vbExclamation
End Sub

Run it from the source workbook: return to Excel and press Alt+F8, select CopyStandardModule, and choose Run. On Mac, Option+F8 commonly opens the Macro dialog, sometimes with the Fn key depending on keyboard settings. The macro identifies the destination by its open workbook name, so change DESTINATION_NAME to the exact name shown in Excel’s title bar. If two open files have the same name, rename one or adapt the selection logic to avoid choosing the wrong destination.

Enable the required VBA project permission

The automated example reads and changes the destination workbook’s VBA project. Excel blocks this type of programmatic access by default in many installations. To enable it on Windows, go to File > Options > Trust Center > Trust Center Settings > Macro Settings, select Trust access to the VBA project object model, and confirm the dialogs. Close and reopen the workbook if needed, then run the macro again.

This setting is distinct from enabling macros in a workbook. It grants programmatic access to VBA projects, so enable it only when you understand why the automation needs it and trust the code that will run. In managed or workplace installations, an administrator may control the setting, and the checkbox may be unavailable. If you do not need automation, use the Visual Basic Editor’s manual copy or the export/import commands instead; those approaches avoid changing this permission for the purpose of a scripted transfer.

Check dependencies and save in a compatible format

After moving a module, check more than whether it appears in the project tree. A procedure can compile only if its referenced procedures, objects, and libraries are available in the destination. For example, code that calls a helper function in another module will not work if you copied only the calling module. Code that uses a worksheet by its VBA code name, a named range, a UserForm, or a workbook-specific event may also need additional components or changes.

  • Review references: In the Visual Basic Editor, use Tools > References to check for any missing library references. Avoid adding a reference just because it exists on the source computer; another computer may not have the same library version.
  • Compile and inspect: In the editor, choose Debug > Compile VBAProject to reveal compile-time issues. Then run the relevant macro on a safe copy and check that it targets the intended sheets and ranges.
  • Preserve VBA when saving: Save the destination as an Excel Macro-Enabled Workbook (.xlsm) or another format that supports VBA, such as Excel Binary Workbook (.xlsb). Saving as .xlsx does not preserve the VBA project; Excel warns that macros will be removed.

A copied module does not automatically make code safe or portable. If the code uses file paths, external connections, hard-coded workbook names, or Windows-specific API declarations, those parts may behave differently on another computer or on Mac. Read those sections before running the macro, especially if the workbook came from an unfamiliar source.

Common problems and practical fixes

The macro says programmatic access is not trusted. Enable the Trust access setting on Windows or switch to manual export/import. If workplace policy controls Trust Center settings, ask the administrator rather than trying to bypass the restriction.

The module is not found. The sample expects a standard module named exactly Module1 in ThisWorkbook. Confirm that the code is running from the source workbook, check the module’s name in the Properties window, and update the constant. A module inside a different open workbook is not automatically the source.

The destination cannot be found. The destination must already be open and its workbook name must match Report.xlsm, including the extension. The example checks open workbooks, not a file path on disk. Open the intended file first, or adapt the macro to open a workbook from a known path and handle file access carefully.

The imported macro does not work. The transfer may have succeeded while a dependency did not. Check for missing procedures, worksheets, named ranges, forms, references, and assumptions about the active workbook. Review whether the code uses ActiveWorkbook where it should use a specific workbook variable; active-window assumptions can cause a macro to edit the wrong file.

The project is locked or read-only. A protected VBA project may prevent inspection or modification, and a read-only destination cannot be saved with the change. Use an authorized, editable copy or ask its owner for access. Do not treat a lock as a reason to circumvent project protection.

Choose the right method for the job

For a single standard module, the Visual Basic Editor copy-and-paste method is direct and lets you compare the source and destination immediately. Export/import is better when you want a reusable file or need to move a component between workbooks at different times. The automated method is useful for repeated controlled transfers, but it adds setup, trust permissions, error handling, and the risk of modifying the wrong project if workbook selection is careless.

If you only want to reuse the macro in future files, consider keeping a separate macro workbook or an Excel add-in rather than copying code each time. A Personal Macro Workbook can store macros for use in Excel on that computer, while an add-in can package reusable functionality for multiple workbooks. These options are not substitutes for transferring workbook-specific code: they require you to account for how the code finds its target workbook and sheets.

Frequently Asked Questions

Can I copy a VBA module from one workbook to another without exporting it?

Yes. Open both workbooks, use Alt+F11, select the module in Project Explorer, and copy and paste it into the other workbook’s project. Exporting to a .bas file is optional, not required.

Does copying a module copy the worksheet or its buttons?

No. It copies code in that module, not worksheet content, form controls, buttons, or other VBA components. Copy those separately if the code depends on them, and verify any assigned button macros after transfer.

Can I copy a module into an .xlsx workbook?

You can work with VBA in the desktop editor, but an .xlsx file cannot retain a VBA project when saved. Save the destination as .xlsm or .xlsb if it must keep the code.

Why is Trust access to the VBA project object model disabled?

Excel restricts programmatic changes to VBA projects as a security measure. On Windows, the setting is in Trust Center Macro Settings; an organization can also manage it by policy. Manual copying or importing may be the better route when you do not need scripted transfers.

Can I use this method in Excel for the web?

No. Excel for the web does not run or edit VBA projects. Open the workbook in desktop Excel for Windows or Mac to use the Visual Basic Editor and transfer VBA code.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.