Scope of VBA
- Last UpdatedJul 18, 2023
- 3 minute read
The scope of an object determines which portions of your code scripts can use that object.
Note: The use of Global, Public, and Private keywords has no effect on scope in VBA.
Procedural (Local) Level Scope
Variables and constants declared (using the Dim, Static, or Const statements) within a VBA procedure (subroutine or function) have local scope to only that within the procedure. This means that procedural level variables and constants cannot be referenced (accessed and used) from anywhere outside of that procedure.
|
|
|
UNINTENDED EQUIPMENT OPERATION Do not use the Global, Public, or Private keywords in your VBA procedures. Using these keywords in procedures can lead to unintended equipment operation when your program is run. Failure to follow these instructions can result in death, serious injury, or equipment damage. |
Procedural level variables declared using the Dim statement do not retain their assigned values when dereferenced. Procedural level variables declared using the Static statement, however, do retain their assigned values between references, even after that procedure moves out of scope.
Modular level scope
Constants declared (using the Const statement) and variables declared (using the Static statement) at the modular level (outside any procedure) in a VBA file have modular scope to all procedures within that same VBA module (file). This means that modular constants and static variables can only be referenced from a procedure located within the same file module, and cannot be referenced from outside of that file module. This has no effect in VBA, even if declared using the Global keyword.
Modular level constants and static variables retain their assigned values for the entire runtime of the project.
Global Level Scope
Variables declared (using the Dim, Global, or Public statements) at the modular level (outside any procedure) in a VBA module (file), have global scope within the Plant SCADA project. This means that modular VBA variables (except statics) can be referenced from both inside and outside of their file module.
Global level variables can be used directly within Plant SCADA command or expression fields.
Procedures (subroutines or functions) declared within a VBA file module, like global variables, have global scope within a Plant SCADA project. They can be referenced or called from any VBA module, as well as from any Plant SCADA command or expression field.
Equally important, all Plant SCADA variable tags, alarm tags, and ActiveX objects are accessible to all VBA file modules (and their procedures) within that project, in the same manner as they have always been accessible to project Cicode files. For information about referencing Plant SCADA project tags using VBA, see Integrating VBA into a Project.
Global level variables will also retain their assigned values between subsequent references, behaving somewhat similarly to the values stored in Plant SCADA tags. In this regard, Global and Public statements are redundant at the modular (global) level in VBA, as they perform the exact same duty as the Dim statement.
Note: If an array is declared with an explicit data type using the Public or Dim keyword at global level, it will result in the error message "Sub or Function not defined". See the example below for more information.
Example
Dim myVarAr(5) As Integer //Results in Error
Dim myVarAr(5) //No Error
Dim daysOfWeek(5) As String //Results in Error
Dim Intvar(5) As Long //Results in Error
Public myArray(5) //No Error
Public myArray2(5) As Integer //Results in Error
Sub TArrayIn()
Dim myVarAr(5) As Integer //No Error
Dim Intvar(5) As Long //No Error
Dim daysOfWeek(5) As String //No Error
Intvar(1)=1
Print Intvar(1)
daysOfWeek(1)= "Naresh"
Print daysOfWeek(1)
myVarAr(2) = 1
Print myVarAr(2)
myArray(1)=3
Print myArray(1)
myArray2(1)=4
Print myArray2(1)
End Sub
