Randall00
Board Regular
- Joined
- Jan 31, 2008
- Messages
- 54
I'm having a hard time understanding how Public variables declared in an Excel workbook retain their values.
Every source I can find suggests that all variables declared as "Public" store values that are available to all procedures across all the workbook modules. These Public variables are supposed to retain their values until the workbook is closed or "End Statement" is used.
I have a procedure sitting in a single module that is set to run when the workbook opens. All it does is dimension two Public variables at the top of the module to refer to the workbook itself and one particular worksheet like this:
There are other procedures in the module that use those variables and when I run them individually, I don't seem to have a problem. However, if I call those procedures from within a UserForm, the Public variables no longer seem to be associated with the workbook and worksheet that I set them to.
In the past, I've worked around this lack of understanding with respect to variable scope by dimensioning the same variables at the top of the form code as well as each and every module in the workbook, but this gets very sloppy and redundant. By the time I've added a handful more procedures into the project, I realize very quickly that I don't have a clue which worksheet is dimensioned to which variable in which module and it seems silly that I should have to continually tell this one workbook that "wsDATA" refers to the DATA sheet!
So anyway, am I missing something about these "Public" variables? All I've been told is that they retain their values until I close the workbook or use "End Statement", yet neither of those things occur in my procedures and the values are clearly not retained....?
Every source I can find suggests that all variables declared as "Public" store values that are available to all procedures across all the workbook modules. These Public variables are supposed to retain their values until the workbook is closed or "End Statement" is used.
I have a procedure sitting in a single module that is set to run when the workbook opens. All it does is dimension two Public variables at the top of the module to refer to the workbook itself and one particular worksheet like this:
Code:
Public wb As Workbook
Public wsDATA As Worksheet
Sub WorkbookVars()
Set wb = ThisWorkbook
Set wsDATA = wb.Sheets("DATA")
End Sub
There are other procedures in the module that use those variables and when I run them individually, I don't seem to have a problem. However, if I call those procedures from within a UserForm, the Public variables no longer seem to be associated with the workbook and worksheet that I set them to.
In the past, I've worked around this lack of understanding with respect to variable scope by dimensioning the same variables at the top of the form code as well as each and every module in the workbook, but this gets very sloppy and redundant. By the time I've added a handful more procedures into the project, I realize very quickly that I don't have a clue which worksheet is dimensioned to which variable in which module and it seems silly that I should have to continually tell this one workbook that "wsDATA" refers to the DATA sheet!
So anyway, am I missing something about these "Public" variables? All I've been told is that they retain their values until I close the workbook or use "End Statement", yet neither of those things occur in my procedures and the values are clearly not retained....?