Chapter 10

Controlling Program Flow and Making Decisions

IN THIS CHAPTER

Bullet Discovering methods for controlling the flow of your VBA procedures

Bullet Finding out about the dreaded GoTo statement

Bullet Using If-Then and Select Case structures

Bullet Performing looping in your procedures

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 some statements, executing some statements multiple times, and testing conditions to determine what the procedure does next. Hang on to your hat and enjoy the ride, because you're about to discover the essence of programming.

Going with the Flow, Dude

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. Table 10-1 provides a sneak preview of these constructs. (These are explained later in this chapter.)

TABLE 10-1 Programming ...

Get Excel VBA Programming For Dummies, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.