Chapter 5
Creating Structured Programs
In This Chapter
Using the
If...Then
statement
Using the
Select Case
statement
Using
Do
loops
Using
For Next
loops
Using the
GoTo
statement
S
tructures
help organize your VBA code so that it can perform more tasks
more efficiently. Special
statements
can help organize the code so that it
accomplishes tasks based on decisions or performs
the same task multiple
times. In this chapter, I demonstrate how to use the special statements used
to make decisions, perform tasks more than once, or redirect control to
another area of the program. The result of using these statements is increased
program
structure, which makes controlling program execution easier.
These statements also improve program flexibility. A program that uses the
right statements can perform a wider variety of tasks and take into account
outside conditions, such as the day of the week or the current state of the
computer. Statements that control program flow are essential to writing pro-
grams that need less input from you and perform more tasks automatically.
For example, VBA can help you create Word documents that automatically
reflect
special days, such as a business anniversary, or Excel reports where
VBA automatically tracks business quarters.
Exercising Control with Structures
Few programs use all the statements in the program file all the time. You
might want the program to perform one task when you click Yes and another
task when you click No. The statements for both tasks appear in the code,
but the program executes only one set of statements.
10_046500 ch05.qxp 12/5/06 5:35 PM Page 111
To control program execution, the developer adds special statements — such
as the
If...Then
statement — that show the beginning
and end of each task
and also decide which task to execute. You might think that letting the com-
puter decide which task to execute would cause the developer to lose control
of the program. However, the developer hasn’t lost control of the program,
because the decision-making process is predefined as part of the program
design.
Most developers refer to the beginning and ending statements for a task as a
control structure
because the statement combination
adds control to a pro-
gram. When you see the
If...Then
statement with its accompanying
End
If
statement in code, the two statements combine to form an
If...Then
structure.
Because the program contains more than one task, it has more than one path
of execution. When
you add control structures,
the number of execution
paths increases exponentially. For example, a program with one control
structure has two paths of execution, but a program with two control struc-
tures has four paths. As you can imagine, the task of debugging the applica-
tion becomes harder when you add control structures, so designing your
program carefully is important.
You can also
nest
control structures. A program might require multiple deci-
sion points to address a specific need. For example, the program might need
to decide whether you requested an apple or an orange. When you select an
apple, the program then might have
to decide between a yellow, green, or red
apple. The program can’t make the second decision without the first, so the
second decision is nested within the first.
Making a Decision with the
If...Then Statement
Most programs require decision-making code. When you need to make the
same decision every time that you perform a task and the outcome of the
decision is always the same, then making the decision is something that you
can tell VBA to do for you by using the
If...Then
statement.
Decision-
making code has several benefits:
Do'stlaringiz bilan baham: