Microsoft Office Tutorials and References
In Depth Information
Variables, Data Types, and Constants
Variable naming conventions
Some programmers name variables so that users can identify their data types by just looking at
their names. Personally, I don’t use this technique very often because I think it makes the code
more difficult to read, but you might find it helpful.
The naming convention involves using a standard lowercase prefix for the variable’s name. For
example, if you have a Boolean variable that tracks whether a workbook has been saved, you
might name the variable bWasSaved . That way, it’s clear that the variable is a Boolean variable.
The following table lists some standard prefixes for data types:
Data Type
Prefix
Boolean
b
Integer
i
Long
l
Single
s
Double d
Currency
c
Date/Time
dt
String
str
Object
obj
Variant
v
User-defined
u
The second example doesn’t declare a data type. Consequently, VBA determines the data type
from the value. The Rate variable is a Double , and the Period variable is an Integer .
Because a constant never changes its value, you normally want to declare your constants as a
specific data type.
Like variables, constants also have a scope. If you want a constant to be available within a single
procedure only, declare it after the Sub or Function statement to make it a local constant. To
make a constant available to all procedures in a module, declare it before the first procedure in
the module. To make a constant available to all modules in the workbook, use the Public
keyword and declare the constant before the first procedure in a module. For example:
Public Const InterestRate As Double = 0.0725
If your VBA code attempts to change the value of a constant, you get an error
( Assignment to constant not permitted ). This message is what you would
expect. A constant is a constant, not a variable.
 
Search JabSto ::




Custom Search