PDA

View Full Version : Scope of PUBLIC Variables



Cyberdude
08-06-2006, 03:43 PM
I?ve never worked with Public variables before, and I?m unable to get a macro in Personal.xls to recognize a Public variable declared in another (open) workbook. The declaration I?m using looks like this:

Public PubVar As Long

Perhaps I am misinterpreting what Help says about Public scope:

?Variables declared using the Public statement are available to all procedures in all modules in all applications unless Option Private Module is in effect; in which case, the variables are public only within the project (http://javascript%3Cb%3E%3C/b%3E:hhobj_27.Click%28%29) in which they reside.?

I understood this to mean that the scope does include multiple open workbooks. If I?m wrong, then the problem is solved. Help defines ?Project? as a set of modules. It didn?t mention whether they have to be in the same workbook or not.

Next question: If I declare the same variable name as Public in two different modules of the same workbook, what are the implications? My tests show that the procedures in each module work ONLY with the variable instance that is declared at the top of their parent module. This seems weird. I would have thought that I?d get an error message showing multiple declarations of the same variable, but I didn?t. When I removed the declaration from one of the modules, then a macro in the other module accessed the remaining Public variable with no problem.

johnske
08-06-2006, 09:30 PM
Hi Sid,

Have a look at Helps 'Understanding Scope and Visibility' for more...

this may help you - Quote: "You can use public procedures, variables, and constants defined in standard modules or class modules from referencing projects. However, you must first set a reference to the project in which they are defined."

Zack Barresse
08-07-2006, 09:32 AM
We wouldn't see that compile error, as technically, it doesn't produce an error (two publically declared variables), it really just changes scope. It's like order of events here. References are processed, any public variables, global constants, API's, priavate variables, procedures, etc.

Cyberdude
08-07-2006, 01:41 PM
Aha! John that bit of info escaped my attention, and to both of you thanx for pointing out these limitations. Interesting. But that sure throws a kink into what I was planning to do. (Phffft!) :beerchug:

Zack Barresse
08-07-2006, 01:43 PM
There is always storing data somewhere on your Personal.xls, or even writing to the registry..