I have a number of public variables that are referenced across several modules. I know if you debug or hit stop the variable gets cleared out. I have been writing these variables to a spreadsheet so I have them in case they get cleared out, but this is messy. I'd rather have it all stored it code. Is there any alternative to a public variable that never gets cleared?
相关问题
- Excel sunburst chart: Some labels missing
- Error handling only works once
- Error handling only works once
- Excel formula in VBA code
- Excel VBA run time error 450 from referencing a ra
相关文章
- Get column data by Column name and sheet name
- programmatically excel cells to be auto fit width
- Unregister a XLL in Excel (VBA)
- Unregister a XLL in Excel (VBA)
- How to prevent excel from truncating numbers in a
- numeric up down control in vba
- Declare a Range relative to the Active Cell with V
- What's the easiest way to create an Excel tabl
Public variables don´t get erased from memory on debug (break) mode if there are still references that point to them further along in your code. In fact, in some cases if you move your mouse over the variable it will tell you the value at the break.
If you want variables to persist I would use the same method as you are currently using (write them down on an excel worksheet) or a database.
If you do write these variables down, I would recommend you never modify the variable directly, instead use setters and getters that suit your model. For example, if you write them down to a worksheet you might use the following:
Here is an example of the CustomDocumentProperties, which I recently started using to store some meta-information (easier than dealing with the CustomXMLParts).
The examples below store only string data, but you can also use date, number and Yes/No (which with some finagling you could sub as a Boolean). You are limited to 255 characters for string data.
You can view the CPD's from the Backstage | Info | Properties | Advanced Properties | Custom:
In the event that you
End
run-time, you can restore the values from the CDP, you can query the property value by:myVar = ActiveWorkbook.CustomDocumentProperties("myProperty").Value
You can use functions like these to set properties in the
CustomDocumentProperties
collection:A simple solution would be to store your variables in the registry, and just read/write them as necessary. This has the added benefit of preserving values over multiple Excel sessions (and even after a computer reboot, or a crash - assuming your registry survived it!).
EDIT: Also see John Walkenbach's book for more information on this.
EDIT: See below comment by Ioannis for an important consideration.
Boilerplate warning: Here be dragons, Twiddle with the Windows registry at your peril, etc etc.
The above warning notwithstanding, realize that almost every program on your Windows computer does something with the registry, and it is not inherently dangerous to do so. Just make sure your code only changes/deletes registry keys which were created by your Excel application.
Example procedures using Windows Scripting (I didn't write these; from a quick search):
Reading from the Registry:
Checking if a Registry key exists:
Saving a Registry key:
Deleting a key from the Registry:
Here is another solution that might be preferable to my first answer (using the registry) depending on the use case.
You could store the values in a "Very Hidden" worksheet. This has the following advantages:
The TempVars Collection is a great alternative to Public Variables. It won't persist between sessions, but it will persist for the entirety of A session.
You will need to add a reference to Microsoft Access 14.0 Object Library though.
TempVars
are only available from 2007 onward.