Understanding VBA Control Structures

Many VBA macros run in a simple fashion. Starting with the Sub statement, they execute every statement in the macro, from start to finish. This is fine as long as you want the macro to do the same thing every time, but sometimes you need a macro that's a little smarter than that. VBA provides a wide variety of program control structures that enable you to change the way the statements are executed, depending upon conditions that are found while the macro is running.

Using the If…Then…End If Statement

Perhaps the simplest and most commonly used control structure in VBA is the If…Then…End If statement. This statement enables you to execute a group of VBA statements only if some condition is true. Consider an example ...

Get Special Edition Using Microsoft® Word 2000 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.