Home > Macros > Basics > Add Code
Add Macro Code to Excel 2003 Workbook
How to copy sample Excel macros to your workbook, and run a macro, in Excel 2003
NOTE: For the newer version of this tutorial,
please visit: Copy
Code to Excel Workbook

|
Copy Excel VBA Code to a Regular
Module
If you find a sample Excel macro on the Internet, you might want to add that code to one of your workbooks.
This video shows how to add that code to a regular module, and there are written instructions below the video.
To add Excel macro code to a regular module, follow these steps:
- Copy the code that you want to use
- Select the workbook in which you want to store the code
- Hold the Alt key, and press the F11 key, to open the Visual Basic
Editor
- Choose Insert | Module

- Where the cursor is flashing, choose Edit | Paste
To run the code:
- Choose Tools | Macro | Macros
- Select a macro in the list, and click the Run button
Copy Excel VBA Code to a Worksheet Module

Some code is Event code, and will run automatically when something
specific occurs in the workbook. For example, if you type in a cell,
and press the Enter key, the worksheet has been changed. This could
trigger the Worksheet_Change event.
Worksheet event code is stored on a worksheet module. To add it to
your worksheet, do the following:
- Copy the code that you want to use
- Select the worksheet in which you the code to run
- Right click on the sheet tab and choose View Code, to open
the Visual Basic Editor.

- Where the cursor is flashing, choose Edit | Paste
Copy Excel VBA Code to a Workbook Module

If the code is Workbook Event code, it should be added to the workbook
code module:
- Copy the code that you want to use
- Select the workbook in which you want to store the code
- Hold the Alt key, and press the F11 key, to open the Visual Basic
Editor
- In the Project Explorer, find your workbook, and open the list
of Microsoft Excel Objects
- Right-click on the ThisWorkbook object, and choose View Code

- Where the cursor is flashing, choose Edit | Paste
Copy Excel VBA Code From a Different Workbook

You may find code in a sample workbook online, and decide to add
it to one of your workbooks.
This video shows how to add that code to a regular module, and there are written instructions below the video.
Follow these steps to copy all the code in a module:
- Open both workbooks
- Hold the Alt key, and press the F11 key, to open the Visual Basic
Editor
- In the Project Explorer, find your workbook, and the workbook
with the code that you want to copy. The screenshot at the right,
the code is in VBACodeCopy.xls and will be copied to MyForm.xlsm
- In the workbook with the code, click the + sign to view the list
of Modules
- Click on the module that you want to copy, and drag it over the
project where you'd like the copy placed.

- Release the mouse button, and a copy of the module will appear
in the workbook.
To run the code:
- Choose Tools | Macro | Macros
- Select a macro in the list, and click the Run button
Related Excel VBA Tutorials
FAQs, Excel Macros
Create an Excel UserForm
UserForm with ComboBoxes
Edit a Recorded Macro
Excel Macros, Getting Started