VBATrain.com

    VBATrain Home    |    Samples TOC    |    Previous    |    Next    


Key Words: about variables

About Variables

If you are a little fuzzy about variables, then the following few points sometimes help to make variables a little clearer.

Variables are words you invent

Variables are words you make up. You store values in them.

In this project, we are requiring variable declaration. That means we must declare all our variables that we use. This approach will allow you to recognize which of the strange "words" you see in a VBA procedure are variables, e.g., see the following screenshot of code that you will write in the Trip Planner project.

making up variables

The thing on the left side of the statement is what changes

In a VBA statement where a value is being assigned, the thing (variable or object property) on the left side of the equation is what changes as a result of the execution of the VBA statement. One way to think of this is to verbalize a VBA equation as "set something equal to something else." Consider, for example, the highlighted line of code shown below.

setting a value

You would read this to mean: Set the value of the variable "iCol" equal to the column number of the named range "_Places". Essentially, you are using the variable to store a number, string, boolean (True or False), date, or array. Variables store values that you want to use later in your procedure or project.

In the above example, the equal sign in the statement is being used to assign a value. An equal sign can also be used to test the equivalency of two values. You will see this in If blocks, as for example,

testing equivalency

Object-type variables require a Set statement

You have to Set an object variable (also called object reference) before you use it. In other words, you have to physically type a Set statement, as shown below.

setting object variables

In the above piece of code, opList1, opList2 and opList3 are all names that were invented to serve as object variables. They are used to store a reference to three different list boxes. When you refer to the object reference in the code, it is the same as referring to the object itself.


    VBATrain Home    |    Samples TOC    |    Previous    |    Next    

Click here for feedback and questions about this chapter.

Copyright © 2006-2008 J. Donald McClenagan, PhD, ALL RIGHTS RESERVED