Chapter 11: Mastering VBA Data Types and Procedures . . . . . . . . . . . . .
421
Using Variables
One of the most powerful concepts in programming is the variable. A variable is a temporary stor-
age location for some value and is given a name. You can use a variable to store the result of a cal-
culation, hold a value entered by the user, or read from a table, or you can create a variable to
make a control’s value available to another procedure.
To refer to the result of an expression, you use a variable’s name to store the result. To assign an
expression’s result to a variable, you use the = operator. Here are some examples of expressions
that assign values to variables:
counter = 1
counter = counter + 1
today = Date()
Figure 11.5 shows a simple procedure using several different variables. Although this is a very sim-
ple example of using variables, it effectively demonstrates just about everything you need to know
about using VBA variables:
FIGURE 11.5
Variable declarations appear at the top of VBA procedures.
l The Dim keyword establishes the new variables — strDocName and strCriteria —
within a procedure.