Читать книгу Excel 2019 Power Programming with VBA - Michael Alexander, Dick Kusleika - Страница 154
Module-wide variables
ОглавлениеSometimes, you want a variable to be available to all procedures in a module. If so, just declare the variable before the module's first procedure (outside of any procedures or functions).
In the following example, the Dim
statement is the first instruction in the module. Both Procedure1
and Procedure2
have access to the CurrentValue
variable.
Dim CurrentValue as Long Sub Procedure1() ' - [Code goes here] - End SubSub Procedure2() ' - [Code goes here] - End Sub
The value of a module-wide variable retains its value when a procedure ends normally (that is, when it reaches the End Sub
or End Function
statement). An exception is if the procedure is halted with an End
statement. When VBA encounters an End
statement, all variables in all modules lose their values.