
38
Macros and User Interaction
38.1 Overview
A macro is a VBA user routine used to automate routine or repetitive
operations in Excel. Macros are also called subroutines; we use the
names interchangeably. Some of the VBA user interaction routines are
covered in sections 38.3 and 38.4. Modules (briefl y mentioned in Chapter
36) are given fuller coverage in section 38.5.
38.2 Macro Subroutines
The fi rst line of a macro subroutine gives the macro a name and lists the
parameters if any. It is very similar to the fi rst line of a function:
Sub MacroName()
The last line, put in automatically by VBA, indicates the end of the
macro and so looks (very appropriately) like this:
End Sub
Separating the fi rst and last line are the statements that the macro
executes. The following is a very simple macro that puts a message on
the screen.
Sub SayHi()
MsgBox “Hi”, , “I say Hi”
End Sub
The subroutine introduces a built-in VBA macro called MsgBox. It
also introduces the way a macro is activated (called) from a VBA routine.
MsgBox is named as a command on a line followed by its list of argu-
ments separated by commas. Notice the syntax: