VB and VBA Users Source Code: Scope of VBA public/global variables
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Scope of VBA public/global variables
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Thursday, April 12, 2001
Hits:
1284
Category:
VBA (Visual Basic for Applications)
Article:
What scope do VBA global variables have and what is the difference between public and global variables. These are probably one of the most common questions sent to our support team. The answer is simple, there is no difference between public and global variables and any workbook which references a workbook exposing global variables, can both see and alter these variables. To reference a workbook, simply open the VBA editor then select "Tools", "References". Now check the name of the workbook's project that you want to reference and click OK. Each workbook is given a default project name of "VBAProject". To save confussion I advise that you make all your VBA Project names unique. Note, that you must also save the workbooks before trying to reference them. Once you have referenced a project you will be able to see all it's public variables, including any public classes. However, you will not be able to create new instances of these classes. The classes in the referenced workbook are effectively, Public Not Creatable, in that you can see them but you can't create new instances of them. If you hit and End statement or press the stop button in the reference project then all the public variables in this project are unloaded/deallocated. If you hit and End statement in the workbook that references this project, then variables will remain allocated. Also note that it is strongly advisable to load/open the workbooks in the order in which they are referenced i.e. don't open a workbook which contains a reference before first opening the referenced workbook.
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder