Tips for copying VBA code to a workbook, and checking the code, to ensure that it runs without errors.
NOTE: If you're using an older version of Excel, click here for the Excel 2003 instructions for copying code to a workbook.
Closes Sunday, Feb. 7th Ė Would you like to learn how to save time with Excel macros? Take a look at the free Mini Course on Macros & VBA -- an excellent video series by Jon Acampora, from Excel Campus. Jonís simple step-by-step approach makes it easy to follow along and learn, so get started now!
To see the steps for pasting a macro into a workbook, and running the macro, please watch this short video tutorial. The written instructions are below the video.
Instead of starting from scratch, if you need an Excel macro, you can often find sample code at reputable sites on the internet. To copy that code, and add it to one of your workbooks, follow these steps:
Another type of Excel code is Event code, which runs automatically when something specific occurs in the workbook. For example, if you enter a number in a cell, or select an entry in a cell's drop down list, the worksheet has been changed. This could trigger the Worksheet_Change event.
Worksheet event code is stored on a worksheet module. To add worksheet event code to your worksheet, do the following:
Another type of code is Workbook Event code, which should be added to the workbook code module:
To see the steps for copying a macro from one workbook to another, in any version of Excel, please watch this short video tutorial. The written instructions are below the video.
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:
To use macros in Excel, you might need to enable them when the file opens. If you are using macros for the first time on your current computer, you might also need to adjust the macro security settings.
Follow the instructions below, to make these changes.
When you open a workbook that contains macros, you might see a security warning, at the top of the worksheet, above the Formula Bar.
If you haven't run macros before, you might need to change your macro security level. (You may have to clear this with your IT department.)
If you copy VBA code into your Excel file, you might need to make changes to the object names, or other settings, so that the code works correctly in your file. Here are three things to check, before you try to run the code in your file:
If there are sheet names or range references in the code, you can modify them, to match your workbook.
These references might be at the top of the procedure, in a Set statement:
Set ws = Worksheets("SalesData")
or elsewhere in the code.
If you run the code without modifying the reference, you might see an error message: Run-time error '9': Subscript out of range
To see where the problem is, click the Debug button, and a line of code will be highlighted in yellow.
To stop the code, click the Run menu, then click Reset.
Change the sheet name in the line that was highlighted, save the changes, and try the code again.
If the code refers to objects on the worksheet, be sure to add those objects in your workbook, and use the correct object name in the code.
For example, in the code for the Data Validation Combo Box, you'll need to add a combo box to the worksheet, and name it as TempCombo. Or, if your combo box has a different name, change the code references to match.
Some code is designed to run when a cell in a specific row or column is changed. For example, in the sample code shown below, there is a red dot on the line that says column 3 is the only one where the change will occur.
NOTE: In all of these examples, you could use Row instead of Column, to limit the target to specific rows.
A) In your workbook, if you want the code to run when a cell in column E is changed, you could change the 3 to a 5.
If Target.Column = 5 Then
B) Or, add more columns in the code. For example:
If Target.Column = 3 _ Or Target.Column = 5 _ Or Target.Column = 6 Then
C) If you don't want to limit the code to a specific column, you could delete the two rows (If...End If) that are marked with red circles. In that case, the code will run for a change in every column.
D) If you want code to run on any column EXCEPT a specific column, use the Not Equal To operator -- <> -- instead of the equal sign. For example:
If Target.Column <> 3 Then
Last updated: February 2, 2016 7:09 PM