I'm a novice VBA programmer and I am working on a workbook that is designed to be run over and over again. To make it more user-friendly, the first time the workbook is opened, a userform is displayed and gathers information from the user and then stores it to a hidden worksheet. For example:
Once the workbook is saved and run again, the saved information is retrieved, like this:
I use this "retrieval" code in various forms and subs that use the information saved in this hidden worksheet.
The whole point of doing this is to prevent the user from having to re-enter the same information each time the workbook is run. They enter it once and then don't have to bother with it again from that point forward.
There are a couple of problems with this approach, however:
1. For every form or sub that uses stored entries, I have to put the code in to retrieve the value (see second code example). This seems redundant.
2. While I am able to "get away" with storing and retrieving values this way in Excel (thanks to having a worksheet I can read/write to), I won't be able to use this approach when using other Office programs that have an entirely different object model.
3. Finally, while this approach works, my gut tells me that this method of retrieving values by object reference is inefficient and slow because there are about 30 values that I store in different cells, many of which are retrieved multiple times by various forms and subs.
I'm hoping there is a better way!
I've been reading up on variable scope and experimenting with it, but I'm still having some problems. I created a basic module (Module1) and declared these variables in the General Declarations section, like this:
My understanding is that this allocates memory for this variable and will make the txtUserName global to my project.
My question, however, is where should I put the code that retrieves the stored entries and stores them in these global variables?
I tried putting it in the Workbook_Open sub, thinking that I could load in all global variables up front when the workbook is opened, but when the sub ends the variables are erased from memory. I believe this is the case because after the workbook is opened, I have a command button that launches a form that uses the txtUserName variable, but the value it is retrieving is "". I also checked for the value in the Immediate Window by typing ? txtUserName and it showed no value.
I also tried declaring the variables in the General Declarations section of Module1 as Static, but I received a compile error saying that I couldn't use Static outside of a procedure.
What I am trying to accomplish is:
1. Declare global variables once and have them accessible from anywhere in my project.
2. Store values and be able to retrieve/transfer them to these global variables so I can use them as needed throughout my project.
3. Ensure these variables are not accessible from outside my project.
4. Make this is a self-contained, standalone workbook. In other words, not use a separate database to read/write to.
Is this possible? Is there a better approach that what I'm attempting to use?
Code:
Worksheets("SETUP").Range("A1").Value = txtUserName
Code:
txtUserName = Worksheets("SETUP").Range("A1").Value
The whole point of doing this is to prevent the user from having to re-enter the same information each time the workbook is run. They enter it once and then don't have to bother with it again from that point forward.
There are a couple of problems with this approach, however:
1. For every form or sub that uses stored entries, I have to put the code in to retrieve the value (see second code example). This seems redundant.
2. While I am able to "get away" with storing and retrieving values this way in Excel (thanks to having a worksheet I can read/write to), I won't be able to use this approach when using other Office programs that have an entirely different object model.
3. Finally, while this approach works, my gut tells me that this method of retrieving values by object reference is inefficient and slow because there are about 30 values that I store in different cells, many of which are retrieved multiple times by various forms and subs.
I'm hoping there is a better way!
I've been reading up on variable scope and experimenting with it, but I'm still having some problems. I created a basic module (Module1) and declared these variables in the General Declarations section, like this:
Code:
Public txtUserName as String
My understanding is that this allocates memory for this variable and will make the txtUserName global to my project.
My question, however, is where should I put the code that retrieves the stored entries and stores them in these global variables?
I tried putting it in the Workbook_Open sub, thinking that I could load in all global variables up front when the workbook is opened, but when the sub ends the variables are erased from memory. I believe this is the case because after the workbook is opened, I have a command button that launches a form that uses the txtUserName variable, but the value it is retrieving is "". I also checked for the value in the Immediate Window by typing ? txtUserName and it showed no value.
I also tried declaring the variables in the General Declarations section of Module1 as Static, but I received a compile error saying that I couldn't use Static outside of a procedure.
What I am trying to accomplish is:
1. Declare global variables once and have them accessible from anywhere in my project.
2. Store values and be able to retrieve/transfer them to these global variables so I can use them as needed throughout my project.
3. Ensure these variables are not accessible from outside my project.
4. Make this is a self-contained, standalone workbook. In other words, not use a separate database to read/write to.
Is this possible? Is there a better approach that what I'm attempting to use?