Variables have a scope; some variables can only be used in functions, while others can be used throughout the program. The lifetime of a variable refers to the period from its creation to its disappearance from memory.
Scope of Variables
【Excel VBA】
In Excel VBA, variables can be divided into global variables, module-level variables, and procedure-level variables according to their scope. The concepts of modules and projects mentioned here can be referred to in Chapter 11.
Global variables are declared and used with the Public keyword in standard modules. Their scope is the entire project. The first letter of a global variable’s name is usually g, for example:
Public gstrVar As String
Although using global variables can bring some convenience, they should be avoided as much as possible because their values can be changed anywhere in the project, which is prone to errors and difficult to troubleshoot. In addition, global variables occupy memory throughout the execution of the program, which may affect the program’s running efficiency.
The scope of module-level variables is their module. They are generally defined at the top of the module using the Private or Dim keyword. They apply to all procedures and functions in the module. The first letter of a module-level variable’s name is usually m, for example:
Private mlngColor As Long
Procedure-level variables are defined in a procedure or function and can only be used in the current procedure or function, having the lowest level.
【Python】
According to the scope of variables, variables can be divided into local variables and global variables. Local variables are variables defined inside a function and are only valid inside the corresponding function. Global variables are variables created outside functions or declared with the global keyword. Global variables can be accessed throughout the program.
Below, we define an f1 function, where v is a local variable whose scope is the inner of the f1 function. The file path is Samples\ch10\Python\sam10-010.py.
v = 10 # Assign 10 to variable v
print(v)
def f1(): # Function f1: assign 20 to local variable v
v = 20
f1() # Call the f1 function
print(v)
In the Python IDLE script window, select Run → Run Module. The IDLE command-line window displays the following results.
>>> = RESTART: .../Samples/ch10/Python\sam10-010.py
10
10
It can be seen that the value of variable v does not change before and after calling the f1 function, i.e., the value set for variable v in the f1 function is only valid inside the function.
Below, we use the global keyword in the f1 function to declare variable v as a global variable, modify its value, and check its scope. The file path is Samples\ch10\Python\sam10-011.py.
v = 10
print(v)
def f1():
global v # Declare v class="text-secondary">as a global variable using the global keyword
v = 20 # Modify the value of variable v to 20
def f2():
print(v) # Output the value of variable v
f1() # Call function f1
print(v)
f2() # Call function f2
In the Python IDLE script window, select Run → Run Module. The IDLE command-line window displays the following results.
>>> = RESTART: .../Samples/ch10/Python\sam10-011.py
10
20
20
It can be seen that since v is declared as a global variable in the f1 function, the value of v changes before and after calling the f1 function. In addition, global variables can be used in other functions.
Lifetime of Variables and Static Variables in Excel VBA
The lifetime of a variable refers to the period from its creation to its disappearance from memory. Therefore, the lifetime of a procedure-level variable in Excel VBA is from its creation to when the procedure execution goes beyond the procedure scope; the lifetime of a module-level variable is from its creation to when the execution goes beyond the current module; the lifetime of a global variable is from its creation to when the program ends.
For procedure-level variables declared with the Dim keyword, these variables exist only when the procedure in which they are located is executing. When the procedure finishes executing, the variable’s value ceases to exist, and the memory occupied by the variable is released. When the procedure is executed again, all its procedure-level variables are reinitialized.
To retain the value of procedure-level variables, they can be defined as static variables. The usage of declaring one or more static variables inside a procedure using the Static keyword is exactly the same as that of the Dim statement, for example:
Static sngSum As Single
Below, we use the AccuSum function to calculate the cumulative sum from 1 to 20.
Private Function AccuSum(intA As Integer) As Integer
Static intSum As Integer
intSum = intSum + intA
AccuSum = intSum
End Function
Since intSum is declared as a static variable, the variable retains its value when exiting the AccuSum function. Add the following test procedure in the same module.
Sub Test()
Dim intI As Integer
For intI = 1 To 10
Debug.Print AccuSum(intI);
Next
End Sub
Running the procedure outputs the cumulative sum from 1 to 10 in the Immediate Window, as follows.
1 3 6 10 15 21 28 36 45 55