Controlling Program Flow and Making Decisions

Some VBA procedures start at the code’s beginning and progress line by line to the end, never deviating from this top-to-bottom program flow.

Macros that you record always work like this.

In many cases, however, you need to control the flow of your code by skipping over some statements, executing some statements multiple times, and testing conditions to determine what the procedure does next.

Some programming newbies can’t understand how a dumb computer can make intelligent decisions.

The secret is in several programming constructs that most programming languages support.

Following table provides a quick summary of these constructs.

Programming Constructs for Making Decisions
Construct How it works
If-Then structure Does something if something else is true.
Select Case Does any of several things, depending on something’s value.
For-Next loop Executes a series of statements a specified number of times.
Do-While loop Does something as long as something else remains true.
Do-Until loop Does something until something else becomes true.

Next post will be about If-Then-Else and Select Case structure.