Search Contextures Sites
Custom Search

More Tutorials

FAQs, Excel VBA, Excel Macros  

Create an Excel UserForm   Video

UserForm with ComboBoxes

Excel VBA Edit Your Recorded Macro

Excel VBA Getting Started  

 

 

 

 

 

 

Adding Code to an Excel 2003 Workbook

Copy Excel VBA Code to a Regular Module video

Copy Excel VBA Code to a Worksheet Module

Copy Excel VBA Code to a Workbook Module

Copy Excel VBA Code From a Different Workbook video

NOTE: For the Excel 2007/2010 version of this tutorial, please visit: Copy Code to Excel Workbook

Copy Excel VBA Code to a Regular Module

You might find sample Excel VBA code in a forum message, or on a web site, and decide to add it to one of your workbooks. Most code can be added by doing the following:

  1. Copy the code that you want to use
  2. Select the workbook in which you want to store the code
  3. Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
  4. Choose Insert | Module
  5. Where the cursor is flashing, choose Edit | Paste

To run the code:

  1. Choose Tools | Macro | Macros
  2. Select a macro in the list, and click the Run button

Watch the Video

To see the steps for pasting a macro into a workbook, and running the macro, please watch this short video tutorial.

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:

  1. Copy the code that you want to use
  2. Select the worksheet in which you the code to run
  3. Right click on the sheet tab and choose View Code, to open the Visual Basic Editor.
  4. 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:

  1. Copy the code that you want to use
  2. Select the workbook in which you want to store the code
  3. Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
  4. In the Project Explorer, find your workbook, and open the list of Microsoft Excel Objects
  5. Right-click on the ThisWorkbook object, and choose View Code
  6. 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. You can copy all the code in a module by doing the following:

  1. Open both workbooks
  2. Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
  3. 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
  4. In the workbook with the code, click the + sign to view the list of Modules
  5. Click on the module that you want to copy, and drag it over the project where you'd like the copy placed.
  6. Release the mouse button, and a copy of the module will appear in the workbook.

To run the code:

  1. Choose Tools | Macro | Macros
  2. Select a macro in the list, and click the Run button

Watch the Video

To see the steps for copying a macro from one workbook to another, in any version of Excel, please watch this short video tutorial.

Related Excel VBA Tutorials

FAQs, Excel VBA, Excel Macros  

Create an Excel UserForm   Video

UserForm with ComboBoxes

Excel VBA Edit Your Recorded Macro

Excel VBA Getting Started  

Get All the Excel News

For regular Excel news, tips and videos, please sign up for the Contextures Excel newsletter. Your email address will never be shared with anyone else.

Search

Search Contextures Sites

 

 

Privacy Policy

 

Contextures Inc., Copyright 2016
All rights reserved.

 

Last updated: May 29, 2016 6:28 PM