Randall00
Board Regular
- Joined
- Jan 31, 2008
- Messages
- 54
My knowledge of VBA is fairly specialized so a lot of the fundamentals aren't quite clear, but this is what I understand about the lifetime of variables and their scope:
A Public variable declared at the top of a module is available to all modules and procedures within that workbook.
Sooooo..:
A Module-Level variable is declared at the top of any module and is available to all procedures in that module. But assuming I'm on the right track above, you still need to set the variables explicitly within each procedure...(right?).
A Procedure-Level variable is declared within the procedure itself and is only available to commands within that procedure. Which should mean that if, within Macro1, you call Macro2, which does not declare those variables, then those variables will not be available to Macro2....err...right?
I hope I'm at least on the right track there, but I've found that UserForms are causing me some grief on this concept. What is a UserForm in the context of the scope of variables?
----
So I guess I'm just trying to understand the relationship between variables declared from a UserForm's code and those declared in other modules. If anyone would be able to provide some insight, that would be excellent--otherwise, I'll be driving myself crazy with trial and error trying to figure it all out.
A Public variable declared at the top of a module is available to all modules and procedures within that workbook.
Sooooo..:
You cannot declare an "application level" variable that is available to all open workbooks and the closest you can get would be explicity declaring and setting the same public variables in the two workbooks in question...right?
Also, even though you declare those variables publicly, you still have to set those variables to their appropriate values in each procedure that uses them...right?
Also, even though you declare those variables publicly, you still have to set those variables to their appropriate values in each procedure that uses them...right?
A Module-Level variable is declared at the top of any module and is available to all procedures in that module. But assuming I'm on the right track above, you still need to set the variables explicitly within each procedure...(right?).
A Procedure-Level variable is declared within the procedure itself and is only available to commands within that procedure. Which should mean that if, within Macro1, you call Macro2, which does not declare those variables, then those variables will not be available to Macro2....err...right?
I hope I'm at least on the right track there, but I've found that UserForms are causing me some grief on this concept. What is a UserForm in the context of the scope of variables?
Are Public variables declared at the top of a module available to UserForms? If so, does that make them available to all UserForms?
Can you declare a Public variable at the top of a UserForm's code? If so, does that make it available to just UserForms, all modules or both?
If you declare a "UserForm"-level variable at the top of a UserForm's code, is it available to all procedures within that code?
Can you declare a Public variable at the top of a UserForm's code? If so, does that make it available to just UserForms, all modules or both?
If you declare a "UserForm"-level variable at the top of a UserForm's code, is it available to all procedures within that code?
----
So I guess I'm just trying to understand the relationship between variables declared from a UserForm's code and those declared in other modules. If anyone would be able to provide some insight, that would be excellent--otherwise, I'll be driving myself crazy with trial and error trying to figure it all out.
Last edited: