Excel VBA General Declarations
If you look again at the top of the coding window, where we had Option Explicit, you’ll see two dropdown lists:
The first dropdown list says General. The second one says Declarations. But the second one only says Declarations because we have clicked the cursor at the top of the coding window. If you click inside of the Sub, the second list changes to the name of that Sub.
You can set up variables at the top of your code, in the General Declarations area. If you do, then those variables can be seen from anywhere in the code for that window. Otherwise, variables are local to whatever Sub or Function you set them up in. For example, our MyNumber variable can only be accessed from the option_explicit_test Sub. If we set up other Subs in this window then MyNumber would not be available to these new Subs. However, if we moved the line Dim MyNumber As Integer to the General Declarations area then other Subs could access the MyNumber variable.
You’ll get some practice with variables set up in the General Declarations in a later Articles.