Microsoft Office Tutorials and References
In Depth Information
Variables, Data Types, and Constants
Another way of data-typing variables
Like most other dialects of BASIC, VBA lets you append a character to a variable’s name to
indicate the data type. For example, you can declare the MyVar variable as an integer by tacking %
onto the name:
Dim MyVar%
Type-declaration characters exist for most VBA data types. Data types not listed in the following
table don’t have type-declaration characters.
Data Type
Type-Declaration Character
Integer
%
Long
&
Single
!
Double
#
Currency
@
String
$
This method of data typing is essentially a holdover from BASIC; it’s better to declare your
variables by using the other techniques described in this chapter. I list these type declaration
characters here just in case you encounter them in an older program.
In the following example, the Dim statement is the first instruction in the module. Both
Procedure1 and Procedure2 have access to the CurrentValue variable.
Dim CurrentValue as Long
Sub Procedure1()
‘ - [Code goes here] -
End Sub
Sub Procedure2()
‘ - [Code goes here] -
End Sub
The value of a module-wide variable retains its value when a procedure ends normally (that is,
when it reaches the End Sub or End Function statement). An exception is if the procedure is
halted with an End statement. When VBA encounters an End statement, all module-wide
variables in all modules lose their values.
 
Search JabSto ::




Custom Search