Macro Source Code

When you record a macro in Excel, the Macro Recorder stores a series of text instructions that describe, in the Visual Basic for Applications programming language, the various actions you perform while the recorder is on. This text description of your commands is called the source code for the macro. Later, when you run the macro, VBA reads the recorded instructions in the source code and executes each instruction in sequence, thereby duplicating the actions that you performed when you recorded the macro.

The following lines list the source code produced when you recorded the FormatArialRedBold12 macro. Notice that the name and the description of the macro that you entered in the Record Macro dialog is included at the beginning of the recorded macro source code, along with any shortcut key you entered.

 1Sub FormatArialRedBold12()
 2'
 3' FormatArialRedBold12 Macro
 4' Format selected cells with Arial bold, red, 12-point font.
 5'
 6' Keyboard Shortcut: Ctrl+Shift+F
 7'
 8    With Selection.Font
 9        .Name = "Arial"
10        .FontStyle = "Bold"
11        .Size = 12
12        .Strikethrough = False
13        .Superscript = False
14        .Subscript = False
15        .OutlineFont = False
16        .Shadow = False
17        .Underline = xlUnderlineStyleNone
18        .Color = 192
19        .TintAndShade = 0
20        .ThemeFont = xlThemeFontNone
21    End With
22End Sub
23      

Don't be dismayed if this source code listing doesn't make a lot of sense to you right now. You can record and execute macros without ever looking at the macro's source code, or even knowing what it does. The next chapter explains the various parts of a macro and also explains how to locate the source code for a particular macro and display it for editing or inspection.