Microsoft Office Tutorials and References
In Depth Information
Numbers Versus Strings
You need to be aware of the difference between a number and a text representation of a number.
In a VBA program, 12345 (without quotes) is a number, whereas “12345” is text. VBA is actually
pretty clever when it comes to figuring out what you mean. For example, look at this code:
Dim s As String
Dim i As Integer
s = “16”
i = s / 2
As its name implies, a constant is program data whose value does not change. VBA has two types
of constants, literal and symbolic. A literal constant is a number or string typed directly into your
code. String literals are typed in double quotation marks. Numeric literal constants are typed
without special formatting. In this code, “Jackson” and 0.75 are literal constants:
Dim Ratio As Single
Dim Name As String
Max = 0.75
Name = “Jackson”
A symbolic constant has a name. To create a symbolic constant, use the Const keyword:
Const CONSTNAME As Type = value
The rules for constant names are the same as for variables. I like to use all uppercase names
with underscore separators for constants, which makes them easily distinguishable from variables
in the code (which are commonly written with a combination of upper- and lowercase). Thus,
PRIME_RATE is easily identified as a constant, and PrimeRate is clearly a variable. The As type
part of the declaration is optional and is required only if you want the constant to be stored as a
specific data type. value is the constant value.
Here are two examples:
Const PRIME_RATE As Double = 0.042
Const FILE_SAVE_PATH = “c:\my documents\databases\”
A big advantage of using symbolic constants is that you can change the value of a constant
throughout the program simply by editing its declaration statement. Another advantage is that the
constant’s name can help in making the program easier to read.
Search JabSto ::

Custom Search