Writing Conditional Expressions

One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression is a part of a complete program statement that asks a true-or-false question about a property, a variable, or another piece of data in a macro. For example, the conditional expression

NumberOfWords < 100 

evaluates to True if the NumberOfWords variable contains a value that is less than 100, and it evaluates to False if NumberOfWords contains a value that is greater than or equal to 100. Table 39-1 shows the comparison operators you can use in a conditional expression.

Table 39-1. The Heart of a Conditional Expression Is the Comparison Operator

Comparison Operator Meaning
= Equal to
< > Not equal to
> Greater than
< Less than
> = Greater than or equal to
< = Less than or equal to

Expressions that can be evaluated as True or False are also known as Boolean expressions, and the True or False result can be assigned to a Boolean variable or property.

Table 39-2 shows some conditional expressions and their results. You'll work with these expressions later in the chapter.

Table 39-2. Sample Conditional Expressions and Their Results

Conditional Expression Result
10 <> 20 True (10 is not equal to 20)
Pages < 20 True if Pages is less than 20; otherwise, False
Application.UserName = "Hugh Victor" True if the registered user name for your copy of Office is Hugh Victor; otherwise, False
Selection.Text = CityName True if the selected text in your Word document matches the contents of the CityName variable; otherwise, False

Running Microsoft Office 2000 Small Business
Running Microsoft Office 2000
ISBN: 1572319585
EAN: 2147483647
Year: 2005
Pages: 228

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net