Creating an addin from an Excel macro
Pages in this article
- Preparations
- Book or add-in
- Menus
- Toolbars
- Limit access
- Protect code
- Initialise
- Save Settings
- UI Languages
- Setup
- Conclusion
Initialization Of Variables
Most utilities will have some settings which are needed throughout their usage and are often read during their initialization phase.
These could include user settings (as discussed in chapter 8), but application settings like the Excel version are often used in various places of the code. Often this kind of information is kept in public variables, which are meant to retain their value during the entire Excel session.
If however the code gets into trouble and causes a runtime error, the user may press "End" and all public variables will loose their values. This is a risky situation, because hitting End will not make menu items inoperable and the utility might be started without the proper settings.
A simple check to ascertain whether everything is still in proper working condition is to add an extra public variable (at the top of a normal module):
Then at the top of each entry routine (routine that is started by an immediate user action):
As soon as the utility has been terminated by an error, or when the utility hasn't been initialized properly, the public variable bVarsOK will be False and the sub InitVars will be started.
Frequently asked Questions
What is the purpose of initializing variables in an Excel macro addin?
Why are public variables used to store application settings in Excel macros?
What happens to public variables when a runtime error occurs and the user presses "End"?
How can you check if the utility's variables are still properly initialized?
What is the role of the public Boolean variable bVarsOK in the macro?
When should the InitVars subroutine be called in the macro code?
What risks are associated with hitting "End" during a runtime error in an Excel macro?
How can user settings be incorporated into the initialization phase of an Excel macro?
Why is it important to retain variable values during the entire Excel session?
What is the significance of starting the InitVars subroutine when bVarsOK is False?


