Microsoft Office Tutorials and References
In Depth Information
5.5 VBA Operators
initialization, since it makes the program less readable and somewhat more prone to logical errors.
Thus, it is a good idea to initialize all local variables explicitly, as in the following example:
Sub Example()
Dim x As Integer
Dim s As String
x = 0 ' Initialize x to 0
s = "" ' Initialize s to empty string
' more code here . . .
End Sub
Note, however, that static variables cannot be initialized, since that defeats their purpose! Thus, it
is important to know the following rules that VBA uses for variable initialization (note also that
they are intuitive):
Numeric variables (Integer, Long, Single, Double, Currency) are initialized to zero.
A variable-length string is initialized to a zero-length (empty) string.
A fixed-length string is filled with the character represented by the ASCII character code
0, or Chr (0).
Variant variables are initialized to Empty .
Object variables are initialized to Nothing .
The Nothing keyword actually has several related uses in Excel VBA. As we will see in Chapter
8 , it is used to release an object variable. Also, it is used as a return value for some functions,
generally to indicate that some operation has failed. Finally, it is used to initialize object variables.
5.5 VBA Operators
VBA uses a handful of simple operators and relations, the most common of which are shown in
Table 5-5 .
Table 5-5 VBA Operators and Relations .
Type
Name
Symbol
Arithmetic Operators
Addition
+
Subtraction
-
Multiplication
*
Division
/
Division with Integer result
\
Exponentiation
^
Modulo
Mod
String operator
Concatenation
&
Logical operators
AND
AND
OR
OR
NOT
NOT
Comparison relations
Equal
=
Less than
<
 
 
Search JabSto ::




Custom Search