Designing procedures
Most of the example programs in this book rely on a single project and a
single module. You might find that most (if not all) of your programs rely on
just
one of each of these blocks, too. When using this strategy, think about
how to break your program into sub-procedures and functions. Use the infor-
mation found in the earlier “Using Subs” and “Using Functions”
sections in
this chapter to determine which kind of module to use.
The important thing when working at the sub-procedure and function levels
is to write code in convenient chunks. Use your sub-procedures and func-
tions as containers to hold a certain kind of task. Think again about Legos.
Every Lego is an individual unit. You need to write sub-procedures and func-
tions so that you can break them apart easily —
making each element an
individual unit. The examples in this book show you various methods for
breaking your code apart because this skill is one of the most important in
programming.
It’s easy to figure out how to break apart simple code. The
MsgBox
function
that you’ve used so often is a good example. Look at how this function works:
You send some information to it, and the function takes
care of all the details
for displaying the message box onscreen. You don’t have to worry about how
MsgBox
accomplishes this task, and there aren’t any weird conditions for
using
MsgBox
.
Writing
statements
After all the other organization is complete, you’re left with one or more pro-
jects that contain one or more modules that contain at least a sub-procedure.
All your Legos are put together, but they’re empty.
The statements that you
write ultimately create the program. All the organization makes the task of
writing the statements easier because you need to concentrate on only one
task at a time.
In Chapter 2, I emphasize the importance of using an organized approach to
writing statements. You begin by creating
a pseudo-code procedure, and
then you change that pseudo-code into statements that VBA can understand.
This process provides two levels of organization for your program: code and
documentation. However, there are two other forms of organization. In the
upcoming “Creating Readable Code” section, I describe the importance of using
white space to make your code readable. In the upcoming “Telling Others
about Your Code” section, I show how to use comments
to describe how your
code works.
70
Do'stlaringiz bilan baham: