Using conditional statements and looping statements (also called control structures), you can write Visual Basic code that makes decisions and repeats actions. Another useful control structure, the WITH statement, lets you to run a series of statements without having to re-qualify an object.
Using Conditional Statements to Make Decisions
Conditional statements evaluate whether a condition is TRUE or FALSE, and then specify one or more statements to run, depending on the result. Usually, a condition is an expression that uses a comparison operator to compare one value or variable with another.
Choosing a Conditional Statement to Use
- ..Then…Else: Branching when a condition is True or False
- Select Case: Selecting a branch from a set of conditions
Using Loops to Repeat Code
Looping allows you to run a group of statements repeatedly. Some loops repeat statements until a condition is FALSE; others repeat statements until a condition is TRUE. There are also loops that repeat statements a specific number of times or for each object in a collection.
Choosing a Loop to Use
- ..Loop: Looping while or until a condition is TRUE
- ..Next: Using a counter to run statements a specified number of times
- For Each…Next: Repeating a group of statements for each object in a collection
Running Several Statements on the Same Object
In Visual Basic, usually you must specify an object before you can run one of its methods or change one of its properties. You can use the WITH statement to specify an object once for an entire series of statements.
- With: Running a series of statements on the same object