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 = (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 builtin VBA functions, Excel's worksheet functions, or custom functions that you develop in VBA. I discuss builtin VBA functions later in this chapter (see "Builtin Functions").
Operators play a major role in VBA. Familiar operators describe mathematical operations, including addition (+), multiplication (*), division (/), subtraction (), exponentiation (°), and string concatenation (&). Lessfamiliar 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 83). And, of course, you can use parentheses to change the natural order of precedence.
Table 83: OPERATOR PRECEDENCE
^ Open table as spreadsheet
Table 83: OPERATOR PRECEDENCE
^ Open table as spreadsheet
Operator 
Operation 
Order of Precedence 
o 
Exponentiation 
1 
* and / 
Multiplication and division 
2 
+ and  
Addition and subtraction 
3 
Operator 
Operation 
Order of Precedence 
& 
Concatenation 
4 
=, <, >, <=, >=, <> 
Comparison 
5 
In the statement that follows, x is assigned the value l0 because the multiplication operator has a higher precedence than the addition operator.
To avoid ambiguity, you might prefer to write the statement as follows:
In addition, VBA provides a full set of logical operators, shown in Table 84. For complete details on these operators (including examples), use the VBA Help system.
Table 84: VBA LOGICAL OPERATORS
^ Open table as spreadsheet
Table 84: 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 gridline 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.
The following instruction uses the Not operator to toggle the gridline 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 Sheetl 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 = "Sheetl" And ActiveCell.Row = l
The following expression performs a logical Or operation. The MsgBox statement displays True when either Sheetl or Sheet2 is the active sheet.
MsgBox ActiveSheet.Name = "Sheetl" _ Or ActiveSheet.Name = "Sheet2"
Responses

jennifer8 years ago
 Reply

CALLIMACO8 years ago
 Reply

Armas Pihlava7 years ago
 Reply

Yonatan4 years ago
 Reply

igor7 months ago
 Reply