Page 100 - Microsoft Office Excel 2003 Programming Inside Out
P. 100
Microsoft Office Excel 2003 Programming Inside Out
Selection Statements
A key facet of most applications is the ability to make decisions. This ability allows the appli
cation to make decisions about input received from the user and any values that might be
calculated.
If…Then…Else Statement
The simplest form of decision making is performed with the If…Then…Else statement. It
examines an expression to see if it is true or not. If the expression is True, the code performs
one set of actions; otherwise, it performs a different set of actions.
If…Then…Else statements can take two forms, either single-line or multi-line. The syntax of
both forms is shown here.
If condition Then statements Else elsestatements
or
If condition Then
[ statements ]
[ ElseIf elseifcondition [ Then ]
[ elseifstatements ] ]
[ Else
[Else elsestatements] ]
End If
● condition A required expression that must evaluate to a True or False Boolean value
● statements Optional block of one or more statements that are to be executed when
the condition is True
● elseifcondition A required expression if ElseIf is present that must evaluate to a True
Chapter 4
or False Boolean value
● elseifstatements Optional block of one or more statements that are to be executed
when the elseifcondition is True
● elsestatements Optional block of one or more statements that are to be executed if no
previous condition or elseifcondition is True
● End If A required element in multi-line form and terminates the If…Then block
Using the If…Then…Else statements requires the use of comparison operators to build the
needed condition statements. The comparison operators compare two or more values and
decide if the values are equal to each other or if one is greater than the other; then the oper
ators return a True or False answer. The six comparison operators are listed in Table 4-11.
74
Part 2: Visual Basic for Applications