Procedure Level Local Variables

From this chapter you already know how to declare a variable using the Dim statement. The position of the Dim statement in the module determines the scope of a variable. Variables declared with the Dim statement placed within a VBA procedure have a procedure-level scope. Procedure-level variables can also be declared by using the Static statement (see "Understanding and Using Static Variables" later in this chapter).

Procedure-level variables are frequently referred to as local variables. Local variables can only be used in the procedure where they were declared. Undeclared variables always have a procedure-level scope.

A variable's name must be unique within its scope. This means that you cannot declare two variables with the same name in the same procedure. However, you can use the same variable name in different procedures. In other words, the CalcCost procedure can have the slsTax variable, and the ExpenseRep procedure in the same module can have its own variable called slsTax. Both variables are independent of each other.

0 0

Post a comment