Expressions

In programming languages, expressions are anything that produces a result. You use expressions to evaluate something, such as a math calculation or a true/false condition. In Visual Basic, the Immediate window functions as an expression evaluator, so it’s a good place to try out different expressions, as shown in Figure 2-13.

Use the Immediate window to quickly evaluate expressions

Figure 2-13. Use the Immediate window to quickly evaluate expressions

Expressions can return values of any type, including arrays or references to objects. The Immediate window can’t display those types, however. Within a program, expressions are usually found to the right of the equals sign:

    res = CubeRoot(42)

But they can also be used as part of any statement that takes a value:

    If IsArray(Selection) Then MsgBox("You selected multiple cells.")

Simple expressions can be combined to form complex ones using operators . Visual Basic provides different sets of operators depending on the type of the expression, as listed in Table 2-9.

Table 2-9. Visual Basic operators

Numeric operators

Comparison operators (return Boolean values)

Logical operators

String operators

Object operators

^

=

And

&

Is

<>

Eqv

Like

Set (assign)

*

<

Imp

= (assign)

 

/

>

Not

  

\

<=

Or

  

Mod

>=

Xor

  

+

    

= (assign)

    

The Like and Is operators in Table 2-9 return Boolean values, but I group them with ...

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.