Microsoft Office Tutorials and References
In Depth Information
Making Decisions in VBA Code
Table 3-3
Logical Operators
Both values are True .
One value is True , or both values are True .
Value is not True .
Exclusive or: One value is True , but both values aren’t True .
The following conditional expression requires that the [Last Name] field
contain “Smith” and the [First Name] field contain “Janet” for the
entire expression to be True:
[Last Name] = “Smith” and [First Name] = “Janet”
An example of an expression that returns True if the State field contains
NJ or NY is the following:
[State] = “NJ” or [State] = “NY”
Using If...End If statements
You can have VBA code make decisions as the code is running, and you can
enable this feature in several ways. One method is to use the If...End If
block of code. The syntax for If...End If looks like this:
If condition Then
End If
condition is an expression that results in True or False, and
statements refers to any number of valid VBA statements. If the condition proves
to be True, the statements between Then and Else execute, and all other
statements are ignored. If the condition proves to be False, only the
statements after the Else statement execute, as illustrated in Figure 3-2.
As an example, imagine that a State variable contains some text. The
following If...End If block checks whether the State variable contains NY.
If the State variable does contain NY, the TaxRate variable receives a value
of 0.075 (7.5%). If the State variable doesn’t contain NY, the TaxRate
variable receives a value of 0.
If State=”NY” Then
End If
Search JabSto ::

Custom Search