Modulewide Variables

Sometimes, you'll 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 MySub and YourSub have access to the CurrentValue variable.

Dim CurrentValue as Integer

Sub MySub()

End Sub

The value of a module-wide variable does not change when a procedure ends. An exception to this occurs if the procedure is halted with an End statement. When VBA encounters an End statement, all module-wide variables lose their values.

0 0

Post a comment