Working with Logical Expressions

A logical expression is an expression that returns a Boolean result. A Boolean value is almost always either True or False, but VBA also recognizes some Boolean equivalents:

■ A False result can be used in an expression as though it were 0. Similarly, you can use 0 in a logical expression as though it were False.

■ A True result can be used in an expression as though it were -1. However, any nonzero value can be used in a logical expression as though it were True.

In Chapter 6, "Controlling Your VBA Code," I'll show you various VBA statements that let your procedures make decisions and loop through sections of code. In most cases, the mechanism that controls these statements will be a logical expression. For example, if x is a logical expression, you can tell VBA to run one set of statements if x returns True and a different set of statements if x returns False.

You'll see that these are powerful constructs, and they'll prove invaluable in all your VBA projects. To help you prepare, let's take a closer look at VBAs logical operators.

0 0

Post a comment