Variables
- Last UpdatedJul 18, 2023
- 2 minute read
Variables are used in VBA to temporarily store data values. Variables let you assign a descriptive name to the data you are working with. You can create a variable once only in your code, and reference (refer to) it thereafter as many times as you like, by using its name in your code in place of the data value. Unlike constants, the value that a variable holds can be changed during the runtime of the project.
All variables declared within a VBA procedure (subroutine or function) have local scope to that procedure only. 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, retain their assigned values between references, even after that procedure moves out of scope.
VBA code used within a Plant SCADA command or expression field is treated as if the command or expression is a separate VBA procedure. Variables declared within such a command procedure have procedural scope and lifetime, as described above.
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). Modular level static variables retain their assigned values for the entire runtime of the project.
Variables declared (using the dim,global,or public statements) at the modular level (outside any procedure) in a VBA file do, however, have global scope within the Plant SCADA project.
Note: Global and public statements are redundant at the modular (global) level in VBA, as they perform the exact same duty as the dim statement.