Page 43 - Excel Progamming Weekend Crash Course
P. 43
d540629 ch02.qxd 9/2/03 9:27 AM Page 18
18 Friday Evening
An opening line that consists of the keyword Sub followed by the procedure name
and parentheses.
A closing line that is always End Sub.
Between the opening and closing lines, one or more lines of code.
You learn more about procedures in Session 7. For now it is enough to understand that
procedures are used to organize VBA code.
When you record a macro (as described later in this session), it is saved as a
procedure. The procedure name is the same name you assigned to the macro.
Note
VBA procedures are stored in workbooks. The code in a single workbook comprises a VBA
project. Within a project, the code is organized in a manner that may seem confusing at
first glance, but after you understand it, provides a lot of flexibility.
Each project is subdivided into a number of components, as follows:
A set of Microsoft Excel objects comprising an object for each worksheet that the
project workbook contains plus the special object named ThisWorkbook. These com-
ponents are always present in a project.
One or more modules.
One or more forms.
One or more class modules.
The last three items — modules, forms, and class modules — are present only if you
explicitly add them to the project (as you learn later in the book).
Each of these components can contain code. When you are writing VBA code, you face
the question of where to place the code. Class modules and user forms are used for special
purposes. Your choices are:
A worksheet. Place code in a worksheet if you want the code associated with
that specific worksheet.
ThisWorkbook. Place code in ThisWorkbook if you want the code associated
with the entire project (workbook).
A module. Place code in a module if there is no specific reason to place it
elsewhere.
Class modules, user forms, and their special purposes are covered in
Sessions 26 and 19 through 21.
Cross-Ref
Where you place your code is not all that critical because the system is designed so that
code is available for use regardless of its location. In addition, it is an easy matter to move
code from one location to another should the need arise. When in doubt, create a module
(explained later in this session) and place the code there.