Assignment Statements


An assignment statement is a VBA instruction that makes a mathematical evaluation and assigns the result to a variable or an object. Excel's Help system defines expression as "a combination of keywords, operators, variables , and constants that yields a string, number, or object. An expression can perform a calculation, manipulate characters , or test data."

I couldn't have said it better myself . Much of the work done in VBA involves developing (and debugging) expressions. If you know how to create formulas in Excel, you'll have no trouble creating expressions in VBA. With a worksheet formula, Excel displays the result in a cell . The result of a VBA expression, on the other hand, can be assigned to a variable or used as a property value.

VBA uses the equal sign ( = ) as its assignment operator. The following are examples of assignment statements (the expressions are to the right of the equal sign):

 x = 1 x =  + 1 x = (y * 2) / (z * 2) FileOpen = True FileOpen = Not FileOpen Range("TheYear").Value = 2007 
Tip  

Expressions can be very complex. You might want to use the line continuation sequence (space followed by an underscore ) to make lengthy expressions easier to read.

Often, expressions use functions. These functions can be built-in VBA functions, Excel's worksheet functions, or custom functions that you develop in VBA. I discuss built-in VBA functions later in this chapter (see "Built-in Functions").

Operators play a major role in VBA. Familiar operators describe mathematical operations, including addition ( + ), multiplication ( * ), division ( / ), subtraction ( ), exponentiation ( ˆ˜ ), and string concatenation ( & ). Less-familiar operators are the backslash ( \ ) (used in integer division) and the Mod operator (used in modulo arithmetic). The Mod operator returns the remainder of one number divided by another. For example, the following expression returns 2 :

 17 Mod 3 

VBA also supports the same comparison operators used in Excel formulas: equal to ( = ), greater than ( > ), less than ( < ), greater than or equal to ( >= ), less than or equal to ( <= ), and not equal to ( <> ).

The order of precedence for operators in VBA is exactly the same as in Excel (see Table 8-3). And, of course, you can use parentheses to change the natural order of precedence.

Table 8-3: OPERATOR PRECEDENCE
Open table as spreadsheet

Operator

Operation

Order of Precedence

ˆ˜

Exponentiation

1

* and /

Multiplication and division

2

+ and ˆ’

Addition and subtraction

3

&

Concatenation

4

= , < , > , <= , >= , <>

Comparison

5

In the statement that follows , x is assigned the value 10 because the multiplication operator has a higher precedence than the addition operator.

 x = 4 + 3 * 2 

To avoid ambiguity, you might prefer to write the statement as follows:

 x = 4 + (3 * 2) 

In addition, VBA provides a full set of logical operators, shown in Table 8-4. For complete details on these operators (including examples), use the VBA Help system.

Table 8-4: VBA LOGICAL OPERATORS
Open table as spreadsheet

Operator

What It Does

Not

Performs a logical negation on an expression

And

Performs a logical conjunction on two expressions

Or

Performs a logical disjunction on two expressions

Xor

Performs a logical exclusion on two expressions

Eqv

Performs a logical equivalence on two expressions

Imp

Performs a logical implication on two expressions

The following instruction uses the Not operator to toggle the grid-line display in the active window. The DisplayGridlines property takes a value of either True or False . Therefore, using the Not operator changes False to True and True to False .

 ActiveWindow.DisplayGridlines = _   Not ActiveWindow.DisplayGridlines 

The following expression performs a logical And operation. The MsgBox statement displays True only when Sheet1 is the active sheet and the active cell is in Row 1. If either or both of these conditions are not true, the MsgBox statement displays False .

 MsgBox ActiveSheet.Name = "Sheet1" And ActiveCell.Row = 1 

The following expression performs a logical Or operation. The MsgBox statement displays True when either Sheet1 or Sheet2 is the active sheet.

 MsgBox ActiveSheet.Name = "Sheet1" _    Or ActiveSheet.Name = "Sheet2" 



Excel 2007 Power Programming with VBA
Excel 2007 Power Programming with VBA (Mr. Spreadsheets Bookshelf)
ISBN: 0470044012
EAN: 2147483647
Year: 2007
Pages: 319

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