Chapter 4: Storing and Modifying Information
09_046500 ch04.qxp 12/5/06 5:54 PM Page 81
constants don’t provide, and you can use variables more often than not to
store your data. However, constants also have distinct advantages, including
Speed:
Using constants can make your application faster. Constants
require less memory, and VBA can optimize your program to perform
better when you use them.
Reliability:
Constants have a reliable value. If a constant has a specific
value when you start the program, that value remains until the program
ends.
Ease of reading:
Most VBA users rely on constants to make their pro-
grams easier to read. The
vbCrLf
constant shown in the examples in
Chapter 3 is the same no matter how many programs you create. Every
developer who sees this constant knows that VBA adds what amounts
to pressing Enter (adding a carriage return and line feed combination)
when you use the constant in an application.
There are other reasons to use constants in place of variables. For example,
Object Browser makes it easy to work with constants. Whenever you high-
light a constant in Object Browser, you see the value associated with that
constant, as shown in Figure 4-1. Notice that the entry at the bottom of
Object Browser tells you that the highlighted entry is a public constant
with a value of
“Hello”
.
Figure 4-1:
Use con-
stants to
provide
quick
access to
standard
values in
Object
Browser.
82
Part II: Learning the Ropes
09_046500 ch04.qxp 12/5/06 5:54 PM Page 82
To see this constant for yourself, load the code example for this chapter. You
can download the code from the Dummies.com Web site at
http://www.
dummies.com/go/vbafd5e
. Choose View
➪
Object Browser or press F2 to dis-
play Object Browser. Select DataTypes in the Library field, as shown in Figure
4-1. Highlight in the Classes list, and highlight MyPublicConstant in
the Members list.
Even though you don’t need to know about them now, VBA supports one
additional constant type: enumerated constants. An
enumerated constant
provides a list of values, and you select one value from the list to use in your
application. The “Using enumerated constants” section of Chapter 8 demon-
strates the use of this constant type. Enumerated constants help document
objects that you create and make them easier to use. Using enumerated con-
stants also reduces the potential for error when someone uses an object that
you create.
Defining scope
Listing 4-1 demonstrates that variable and constant scope rules work about
the same. Always use
Private
to keep a variable or constant hidden from
the outside world. Use
Public
to provide access to the variable or constant
outside the current module. Any variable or constant that you define within a
Sub
or
Function
is always private to that
Sub
or
Function
. See the “Getting
the Scoop on Scope” section of Chapter 3 for additional information.
Defining the Data Types
A
data type
is a method of defining data to make it easier to work with in a
program. The computer still sees the data as a series of bits, but VBA works
with different data types in different ways. The computer can see the binary
value, 1000001b, but it doesn’t do anything special with that value. VBA can
see this binary value as the number 65 or the letter
A
depending on the data
type that you assign to the value. The data type is important in understand-
ing the value and working with it. Using a data type also ensures that the
program follows certain rules. Otherwise, the data could become corrupted
because the program could mishandle it.
Although a variable in general is simply a box for storing data, you can think
of these data types as special boxes for storing specific kinds of data. Just
as you would use a hatbox to store a hat and not a car engine, you use these
special box types to store particular kinds of data. For example, you use a
string to hold text, not logical (true/false) values.
VBA supports a number of standard data types, including
Byte
,
Boolean
,
Integer
,
Long
,
Currency
,
Decimal
,
Single
,
Double
,
Date
,
String
,
83
Do'stlaringiz bilan baham: |