Chapter 4
Storing and Modifying
Information
In This Chapter
Using variables in programs
Using constants in programs
Working with various data types
Changing
data by using operators
Designing a report for Excel
I
n this chapter, I refine the concept of a variable by describing variable
types and how you can modify their content. Understanding how a com-
puter stores information is very important. Computers don’t see information
the same way that you do — many methods of representing
information that
seem obvious to you are invisible to the computer.
As far as the computer is concerned, everything is a series of bits that it has
to move around. Data types were invented, sometimes by trial and error, to
make the bits easier for humans to understand. For example, when you see
the
letter
C
on the display, all that the computer sees is a series of 8 bits
that form a special number. Interpretation of this number as the letter
C
is
for your benefit. Understanding that everything is a series of bits makes this
chapter easier to understand.
When you begin to realize that you’re in control — that you decide how to
organize and interpret the bits the computer is moving around for you —
you’ve taken a large step in understanding how VBA works. VBA helps you
tell the computer how you want the bits moved around.
In the following sec-
tions, I describe various ways in which you can interpret and modify the bits
that the computer stores for you.
09_046500 ch04.qxp 12/5/06 5:54 PM Page 79
Understanding Variables and Constants
VBA provides many levels of data interpretation. Some data interpretations
help you make your program reliable, others make it run faster, still others
provide
accuracy, and a few make the data easier to interpret. One of the two
big distinctions is between variables and constants. You can modify a
variable
anytime the program can actually access it. A
constant,
however, retains the
same value all the time. You use variables as storage containers for data that
changes, and you use constants for data that doesn’t change. Variables are
more
flexible than constants are, but constants make your program run
faster, so each type of storage has its place.
Making the declaration
All data in a VBA program looks like a series of bits to the computer. The
computer doesn’t understand the difference between a constant and a
variable — VBA provides this distinction for your benefit. You use different
methods to mark variables and constants in your program.
Variables have a scope and a data type. See the “Defining the Data Types” sec-
tion, later in this chapter, for data type descriptions and how to use them. See
the “Getting the Scoop on Scope” section of Chapter 3 for scope issues.
Declare
variables with both scope and data type so that VBA knows how to work with
them. Constants can also have a scope, but they don’t have a data type associ-
ated with them. A special constant type,
#Const
, lets you define conditional
constants — those that help you tell the compiler
how to compile an applica-
tion. Listing 4-1 shows some examples of variable and constant declarations.
Do'stlaringiz bilan baham: