How to Copy and Paste VBA Code into Excel

VBA & macros · Updated November 3, 2026

How to Copy and Paste VBA Code into Excel

To copy and paste VBA code into Excel, open the Visual Basic Editor, insert a standard module, paste the code into its code window, and save the workbook as an Excel Macro-Enabled Workbook (.xlsm). On Windows, press Alt+F11 to open the editor; on Mac, use Developer > Visual Basic or Option+F11 (you may need to hold Fn). Excel for the web cannot run or edit VBA macros, so use the desktop app.

This guide is for Microsoft Excel for Microsoft 365 on Windows, with Mac differences noted where they matter. You will also see a small, harmless example macro and how to run it. VBA code can automate actions, but a macro may change or delete workbook data; only run code you understand and trust.

What You’ll Need

  • Excel for Microsoft 365 desktop on Windows or Mac. VBA macros are not supported in Excel for the web.
  • The VBA code you want to use, copied from a source you trust. If you are adapting code, keep any instructions about required sheets, cells, or references nearby.
  • A workbook saved in a location where you can create or save files. To keep VBA code in the workbook, save it as an .xlsm file.
  • A backup or separate copy of the workbook before running a macro that changes data. Excel macros cannot be undone with Ctrl+Z.

Step-by-Step

  1. Open the workbook in desktop Excel. If the file is currently open in a browser, choose the option to open it in the desktop app. VBA is not available in Excel for the web. Before editing, consider using File > Save As to create a separate working copy, especially if the macro will edit or delete cells.
  2. Show the Developer tab if it is missing. On Windows, select File > Options > Customize Ribbon, check Developer in the Main Tabs list, and select OK. On Mac, open Excel > Preferences (or Settings) > Ribbon & Toolbar, select or check Developer, and save the change. Menu wording can differ slightly between Mac releases. If Developer is already visible in the ribbon, skip this step.
  3. Open the Visual Basic Editor. On Windows, press Alt+F11. You can also choose Developer > Visual Basic. On Mac, select Developer > Visual Basic; Option+F11 may also open it, though some keyboards require Fn+Option+F11. The editor opens in a separate window. If you do not see your workbook in the Project Explorer, press Ctrl+R on Windows or use View > Project Explorer. On Mac, use the editor’s View menu if the panel is hidden.
  4. Select the correct workbook project. In the Project Explorer, look for a project named something like VBAProject (YourWorkbook.xlsm). Select the project belonging to the workbook where the code should live. If several workbooks are open, check the filename carefully; putting code in another workbook can make it appear to vanish when you close that file. If the Project Explorer is hidden, use View > Project Explorer in the editor.
  5. Insert a standard module. With the correct workbook project selected, choose Insert > Module in the Visual Basic Editor. A new item such as Module1 should appear under the Modules folder, and a blank code window should open. A standard module is the right place for most standalone macros that begin with Sub and end with End Sub. Do not paste a general-purpose macro into a worksheet’s code window unless its instructions specifically say to use that sheet’s events or code module.
  6. Copy and paste the code into the module window. Select only the VBA code, not explanatory text, web page formatting, or code fence markers. Copy with Ctrl+C on Windows or Command+C on Mac, click inside the blank module window, and paste with Ctrl+V or Command+V. If the code is supplied in multiple parts, paste the parts in the order given and check that each procedure has its matching End Sub or End Function. Keep indentation if it is included; indentation helps readability even though VBA does not require it.
  7. Check the pasted code before running it. The following example displays a message and does not change worksheet cells. It can help confirm that the editor, module, and run command are working:
Sub ShowWelcomeMessage()
    ' Display a simple message to confirm the macro runs.
    Dim messageText As String
    messageText = "The VBA code is running."
    MsgBox messageText, vbInformation, "VBA Example"
End Sub

In the example, the procedure starts with Sub and finishes with End Sub. The variable messageText is declared as a String, then the MsgBox line displays its value. If you paste the example, compare it with the code window and make sure the quotation marks are straight double quotes. Curly quotation marks copied from formatted text can cause a compile error. If you are using different code, do not replace it with this example unless you only want to test the basic setup.

  1. Run a compile check where possible. In the editor, choose Debug > Compile VBAProject. This checks the project for some syntax and declaration errors, but it does not prove that a macro is safe or that it will produce the intended result. If the command is unavailable, Excel may have nothing new to compile, or the project may already be compiled. Read any highlighted line and error message before making a careful correction.
  2. Save the workbook in a macro-enabled format. Return to Excel and choose File > Save As. Select Excel Macro-Enabled Workbook (*.xlsm) as the file type, choose a location, and save. If you save as .xlsx, Excel cannot retain VBA code in that workbook and may warn that the macro project will be removed. Choose the macro-enabled format when you need the code to remain in this file. For code intended to be available in every workbook, a Personal Macro Workbook is another option, but it is a separate setup and is not needed for a first macro.
  3. Run the macro only after reviewing it and making a backup. The sample above only displays a message. To run a macro from the editor, click inside its procedure and press F5, or use Run > Run Sub/UserForm. You can also return to Excel, choose Developer > Macros, select the macro name, and click Run. On Windows, Alt+F8 opens the Macro dialog; Mac shortcut behavior can vary, so use Developer > Macros if needed. For macros that alter a workbook, save a separate copy first: Ctrl+Z (or Command+Z on Mac) does not reverse VBA actions.
  4. Close and reopen the file to confirm it was saved. Save your changes, close the workbook, and reopen the .xlsm file if you need to verify persistence. Excel may show a security notification about macros. Only enable content if you trust the file and its source. If the code is missing after reopening, check that you saved the correct workbook as .xlsm and pasted the code into that workbook’s project rather than another open project’s module.

Common Mistakes

Pasting into the wrong place: The worksheet grid is not the VBA code editor. Open the Visual Basic Editor and paste into a module’s code window. Also distinguish a standard module from Sheet1 or ThisWorkbook. A regular macro generally belongs in a standard module; event procedures, such as code that runs when a sheet changes, may need to go in the relevant worksheet module instead.

Saving as .xlsx: The .xlsx format does not store VBA macros. If Excel warns that the VBA project will be removed, stop and save a macro-enabled .xlsm copy instead. Do not dismiss the warning if you still need the code.

Copying extra characters or only part of a procedure: Avoid copying explanatory paragraphs or formatting markers along with the code. Check that the procedure begins with its Sub or Function declaration and includes the corresponding End Sub or End Function. A missing line can prevent compilation.

Running code without checking its effects: A pasted macro can overwrite cells, delete sheets, or interact with files. Read it first, check which workbook and range it refers to, and test it on a copy. Never enable macros from an unknown email attachment just because Excel displays an Enable Content button.

Tips

Keep the original code in a separate text file or note before editing it. That gives you a clean version to compare against if a change introduces an error. Make one change at a time, save, and use Debug > Compile VBAProject to catch some syntax problems early. Add brief comments with an apostrophe to explain what an unfamiliar section is meant to do; comments help readers and do not execute as code.

Use clear module and procedure names when the code will be maintained. In the Project Explorer, you can select a module and press F4 to show the Properties window, where the module’s Name property can be changed to a descriptive name without spaces. Avoid changing variable names or workbook references casually: other lines may depend on them. Keep a backup copy before testing changes, even if the first run appears harmless.

If you need to move the macro to another workbook, copy the procedure from the module in the editor and paste it into a standard module in the other workbook’s project. Then save that workbook as .xlsm too. Copying only the worksheet cells will not transfer VBA code. If the macro depends on named ranges, other modules, or a reference library, those dependencies may need to be copied or configured as well.

Troubleshooting

The Visual Basic Editor will not open: Confirm you are using the desktop version of Excel, not Excel for the web. Try Developer > Visual Basic rather than the keyboard shortcut. On Windows, check that Alt+F11 is not intercepted by another utility. On Mac, try Developer > Visual Basic or Option+F11 and, if necessary, Fn+Option+F11. If Developer is absent, enable it using the ribbon settings described above.

You cannot see a blank code window or the project: In the editor, select the workbook project in Project Explorer and choose Insert > Module. If the Project Explorer is hidden, use View > Project Explorer. If the code window is behind another window, use the editor’s Window menu or select the module in Project Explorer and open it. Make sure the module belongs to the intended workbook.

Excel says macros are disabled: First verify that the file and its source are trustworthy. Excel’s security settings may block macros or show a warning when a file opens. Follow your organization’s policy, and ask an administrator if the device is managed. Do not lower security settings globally or enable unknown macros just to make a copied procedure run. If the file came from the internet, Windows may mark it as downloaded; follow Microsoft’s and your organization’s guidance rather than bypassing protections without review.

You get a compile error or a line is highlighted: Read the full error message and inspect the highlighted line plus the lines immediately above it. Common causes include missing End Sub, unmatched parentheses, misspelled procedure names, curly quotes, or code pasted into the middle of another procedure. If the code uses a library or a function not available in your Excel environment, consult its source for the required setup. Correct one issue at a time, then compile again.

The macro runs but nothing seems to happen: Confirm you selected the right macro in Developer > Macros and that it belongs to the open workbook. Some procedures need a particular sheet to be active, a cell selection, or input data. Read the code for those assumptions. For the example macro, the expected result is a message box; if no message appears, check that the procedure name is selected and that the code is in a standard module.

The code disappeared after saving: Check the file extension and reopen the workbook you actually saved. An .xlsx copy cannot retain VBA. Also check whether the code was pasted into a different open workbook or into an unsaved project. Save the correct workbook as .xlsm, then verify the module appears under that workbook in Project Explorer.

Frequently Asked Questions

Can I paste VBA code directly into an Excel cell?
No. Cells store worksheet values and formulas, not VBA procedures. Paste the code into a module’s code window in the Visual Basic Editor. A cell can contain text that looks like code, but that will not make it a runnable macro.

Can I use VBA in Excel for the web?
Excel for the web can open and edit many .xlsm workbooks, but it cannot create, edit, or run VBA macros. Open the workbook in the desktop Excel app to work with its VBA project. Office Scripts are a different automation feature, not a way to run pasted VBA in the browser.

Do I have to save the file as .xlsm?
If the VBA code needs to stay inside that workbook, yes: save it as an Excel Macro-Enabled Workbook (.xlsm). If you save as .xlsx, the macro project is not retained in that file. A Personal Macro Workbook is a separate option for macros intended for your Excel installation rather than one particular workbook.

Why does Excel show a security warning when I open the file?
Excel restricts macros because VBA can make changes or perform actions on your computer. Enable macros only when you trust the workbook and understand its source and purpose. If you are unsure, leave macros disabled and ask the sender or your administrator to verify the file.

Can I undo a macro with Ctrl+Z?
No. Excel’s regular Undo history does not reverse actions performed by VBA. Save a separate copy before running a macro that changes data, and test unfamiliar code on sample data or a duplicate workbook.

Will the same shortcut work on Mac?
Not always. Windows users can use Alt+F11 for the editor and Alt+F8 for the Macro dialog. On Mac, use Developer > Visual Basic and Developer > Macros; Option+F11 may open the editor, sometimes with Fn. Ribbon commands are the dependable alternative when a keyboard shortcut differs.

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.