My VBA application consists of many procedures stored in several modules in one workbook. I have 2 key public variables:
They are declared at the top of a normal module that contains the procedure that [re-]initializes them. This procedure is always called when the application is first opened but not (normally) again during an Excel session.
There are no procedures that that do anything to change them.
The whole application works as a series of nested loops, each headed by an Inputbox that allows the user either to change to a different option or to cancel the loop and go back up to the higher level.
Cancelling at the highest level simply returns control to Excel without closing the currently-opened data book or clearing the public variables. While I am developing the application and testing it, I very often cancel at the highest level and then restart at the second level down - which is where the problem occurs:
What is it that causes Public variables to lose their values? They are declared in a way that allows them to retain their values in all the connected procedures as long as the application is not reset by closing a top-level procedure.
Is this something that could be cured by moving or copying the Public declaration into other modules?
Or must I specifically reset their values when restarting the application at a lower level?
- a Workbook object that points to any one of several data books.
- a String variable holding the basic name of the current data book.
They are declared at the top of a normal module that contains the procedure that [re-]initializes them. This procedure is always called when the application is first opened but not (normally) again during an Excel session.
There are no procedures that that do anything to change them.
The whole application works as a series of nested loops, each headed by an Inputbox that allows the user either to change to a different option or to cancel the loop and go back up to the higher level.
Cancelling at the highest level simply returns control to Excel without closing the currently-opened data book or clearing the public variables. While I am developing the application and testing it, I very often cancel at the highest level and then restart at the second level down - which is where the problem occurs:
[*=1]The public Workbook object is now Nothing
[*=1]the public String variable is now empty.
even though the previously-used data book is still itself open!
What is it that causes Public variables to lose their values? They are declared in a way that allows them to retain their values in all the connected procedures as long as the application is not reset by closing a top-level procedure.
Is this something that could be cured by moving or copying the Public declaration into other modules?
Or must I specifically reset their values when restarting the application at a lower level?
Last edited: