VBA Blogs: Going Through the Visual Basics – Part 9
12 October 2018
We thought we’d run an elementary series going through the rudiments of Visual Basic for Applications (VBA) as a springboard for newer users. This blog looks at the concept of a variable scope at a project level.
Variables can also be used in different subroutines and functions. If there are items that are known to have constant values throughout the entire workbook, they can be declared explicitly in one place for easy reference.
Project Level
Sharing variables between procedures within the same module was very simple but what if another module wanted to reuse the same variable?
Module2 wants to use myTestString but it is declared in Module1.
Upon running, it again says the ‘Variable not defined’. In order to force a variable to be accessible to the entire workbook the statement, the ‘Public’ statement may be used.
Now this will be accessible from Module2 so when the following code is called.
The variable is used as necessary and the expected result eventuates.