How To: Use Someone Else’s Macro
by Jon Peltier
Peltier Technical Services, Inc., Copyright © 2009.
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
This is the first in a series of How To posts that are targeted at Excel users who are unfamiliar with VBA programming. Topics will include
Suppose you need Excel to do something beyond its normal capabilities, or just do something automatically to save you from hours of tedium. The answer is often a VBA procedure, or “macro”. You’re not a programmer, you don’t know a macro from a mackerel, so what do you do? You could ask a colleague, you can Google for help, you can ask on a forum or newsgroup. And the answer is, “Use this macro.” Oh boy. Now what do you do?
Let’s put the macro into your workbook and try it out. We’ll use the following simple macro for this exercise.
Sub TestMacro()
' center alignment
Selection.HorizontalAlignment = xlCenter
' font: arial, 10 point, bold, blue color
With Selection.Font
.Name = "Arial"
.Size = 12
.Bold = True
.ColorIndex = 5
End With
End Sub
The first step to using a VBA procedure is to open the VB Editor (VBE). There are three ways to get there:
- Tools menu > Macro > Visual Basic Editor
- Visual Basic Editor button on the Visual Basic toolbar
- Shortcut key combo Alt+F11

Find your workbook in the Project Explorer window. By default, the Project Explorer is docked along the left edge of the VBE window. If you don’t see it, activate it from View menu > Project Explorer.

Note the treeview of the workbook. The top level is the VBAProject, with the workbook name in parentheses. Under that is a folder entitled Microsoft Excel Objects. Within this is an element for the workbook (ThisWorkbook) and for the worksheet, with the sheet name in parentheses. There would be an element in this list for every worksheet and chart sheet in the workbook.
We need to insert a code module. This is easy: Insert menu > Module.

A new folder, Modules, has opened under the VBAProject entry in the treeview, and an item called Module1 appears in the folder. A blank code module window has opened in the VB Editor.

Copy the macro code from wherever you found it, and paste it into this window. Red text indicates a syntax error, so you will need to figure out what’s wrong. Often it’s just a problem with line wrapping in email or on a web page.

This macro requires a range to be selected, so go back to Excel and select a range, then run the macro. This can be done in a number of ways:
- Tools menu > Macro > Macros, select the Macro, and click Run
- Shortcut Key combo Alt+F8, select the Macro, and click Run
- Run Macro button on the Visual Basic toolbar, select the Macro, and click Run
- Click in the macro with the mouse and press F5


This is the first exposure most people have to VBA and the VB Editor. In a series of upcoming posts, I’ll cover the following VB topics:
- Recording your own macro
- Fixing a recorded macro
- Assigning a macro to a button or shape, ActiveX button, or menu or toolbar
- VBE settings
- VBE features
- Debugging and error proofing
Possibly Related Posts:
- How To: Record Your Own Macro
- How To: Assign a Macro to a Button or Shape
- How To: Assign a Macro to a Toolbar or Menu
- How To: Fix a Recorded Macro
- Improved Macro Security Warning in Excel 2010
- How To: Assign a Macro to an ActiveX Control
- Highlight a Series with a Click or a Mouse Over
- Chart Event Class Module to Highlight a Series
- VB Editor Settings
- Indispensable Excel Utilities
Posted: Sunday, March 9th, 2008 under VBA.
Comments: 8
Comments
Comment from Dan
Time: Monday, March 10, 2008, 12:29 am
You know Jon, I’m going to take a tangent on this topic. I am an experienced programmer (in other languages) and it seems like every VBA lesson out there assumes that you have no knowledge whatsoever. Are you aware of some better advanced material (i.e., I know how to program and now I want to know how to connect to a SQL server and a few excel sheets on networked drives and then perform data validation and calculations on the combined data with VBA). Sometimes I think it would be faster to figure out the COM model and just use IronPython :P
Excel’s object model is a bit quirky and it’s implementation of OOP seems to be lacking, where is the material on that stuff?
Thanks
Comment from derek
Time: Monday, March 10, 2008, 2:36 am
I look forward to this series. VBA is the frontier I just cannot seem to cross. I can do a lot with “charts and things” without VBA, but usually it means I have to maintain my sreadsheets myself. If I could turn my knowledge into robust simple programs, I could hand more work off to my colleagues :-)
Comment from Jon Peltier
Time: Monday, March 10, 2008, 5:53 am
Derek – Charting was the last area of VBA I got into. There was something scary about it. Or that I was losing control. But I got over it, got into the branch of the object model that dealt with charts, and despite some funny business there, realized it’s really no different that the rest of it.
Dan – The advanced work is not readily available as tutorials. The examples in advanced Excel VBA is there, but hard to find. You might find it useful to expand your search to VB6: those resources are still available despite dot-net having taken over. There are a small number of advanced books. Walkenbach’s books range from beginner to more-than-intermediate, while advanced topics and details are found in the Excel VBA Handbook series and the seminal Professional Excel Development (look for authors Bullen, Bovey, and Green together, and occasional others).
Here is a link to a number of books about programming in Excel and VBA:
http://peltiertech.com/Excel/xlbooks.html#ExcelVBA
And here are the most advanced Excel VBA books you will find:
Comment from derek
Time: Monday, March 10, 2008, 4:12 pm
Just to be clear, I wasn’t saying I’m new to VBA as it applies to charts. I was saying I’m new to VBA, period.
Comment from Jon Peltier
Time: Monday, March 10, 2008, 4:56 pm
Derek – VBA isn’t too tough. The macro recorder (see the next post) gives you much of what you need, then it’s a matter of fiddling around, or using Google, to make it do wyat you need.
Comment from Jorge Camoes
Time: Monday, March 10, 2008, 5:40 pm
Jon, I believe Derek is the user Charley Kyd talks about…
Derek, believe me, I am not a programmer, and I don’t go much beyond the examples above, but once you start automating some tasks with recorded macros you will not turn back. There are many things that you simply can’t do without VBA (like stupid and repetitive tasks and user interfaces).
Comment from Jon Peltier
Time: Monday, March 10, 2008, 6:51 pm
Note to self: blog about how I got started with Excel programming.
Jorge – I’ve heard it said that laziness is the mother of all inventions, which may be true. I know for me, laziness is why I got started programming. Back in the old days of Excel 4, I had this technique I used to model material deformation behavior, and it required iteration between two Solver loops, one for loading and the other for unloading. You had to run alternative Solver simulations on each part until the difference between them was less than some small acceptable error, which meant probably a total of a dozen or so Solver loops, and you had th change all of the constraints and target cells and changing cells each time. Then repeat for a total of a dozen cases. So let’s say 144 Solver loops, each set up manually. This took more than half a day to do by hand, and it was tedious as hell, almost as tedious as this description. Anyway, I spent a couple weeks in Excel with the XLM manual in my lap, and I automated the sucker, and finally it ran in about two minutes. When I got Excel 97, this was my first VBA project, and it was hard, but I got the time down to ten or fifteen seconds. There was no turning back.
Comment from Dan
Time: Tuesday, March 11, 2008, 12:42 am
Thanks for the pointers (no pun intended) Jon.



















Write a comment
I welcome comments from my readers. If you have an opinion on this post, if you have a question or if there is anything to add, I want to hear from you. Whether you agree or disagree, please join the discussion.
If you want to include an image in your comment, post it on your own site or on one of the many free image sharing sites, and include a link in your comment. I'll download your image and insert the necessary html to display the image inline.
Read the PTS Blog Comment Policy.