How to store PUBLIC variable values for later use in different subroutines?

simurq

Board Regular
Joined
Nov 11, 2011
Messages
73
According to J. Walkenbach:
...if the procedure is halted by an End statement, static variables do lose their values.

That's exactly what happens when I check the value of any public and static variables in the Watch window.

Code:
[COLOR=darkblue]Public[/COLOR] slxRibbon [COLOR=darkblue]As[/COLOR] IRibbonUI[COLOR=darkblue]Public[/COLOR] [COLOR=darkblue]Declare[/COLOR] [COLOR=darkblue]Function[/COLOR] InternetGetConnectedStateEx [COLOR=darkblue]Lib[/COLOR] "wininet.dll" ([COLOR=darkblue]ByRef[/COLOR] lpdwFlags [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR], [COLOR=darkblue]ByVal[/COLOR] lpszConnectionName [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], [COLOR=darkblue]ByVal[/COLOR] dwNameLen [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR], [COLOR=darkblue]ByVal[/COLOR] dwReserved [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]) [COLOR=darkblue]As[/COLOR] Long
[COLOR=darkblue]Public[/COLOR] [COLOR=darkblue]Const[/COLOR] sLogFile [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR] = "mylogfile.log"
[COLOR=darkblue]Public[/COLOR] [COLOR=darkblue]Const[/COLOR] sSettingsFolder [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR] = ".settingsfolder"
[COLOR=darkblue]Public[/COLOR] [COLOR=darkblue]Const[/COLOR] sSettingsFile [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR] = "settings.cfg"
[COLOR=darkblue]Public[/COLOR] [COLOR=darkblue]Const[/COLOR] slxVersion [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR] = "v3.0 Build 011118"
[COLOR=darkblue]Public[/COLOR] bReadOnly [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Boolean[/COLOR]
[COLOR=darkblue]Public[/COLOR] sOSVersion [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
[COLOR=darkblue]Public[/COLOR] dXLVersion [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Double[/COLOR]
[COLOR=darkblue]Public[/COLOR] dVBEVersion [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Double[/COLOR]
[COLOR=darkblue]Public[/COLOR] bInternetConnStatus [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Boolean[/COLOR]

[COLOR=darkblue]Sub[/COLOR] AddInInitialise(Ribbon [COLOR=darkblue]As[/COLOR] IRibbonUI)
[COLOR=green]' Checking code goes here...[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub

[/COLOR][COLOR=darkblue]Sub[/COLOR] AnotherSubroutineInAnotherModule()
[COLOR=green]'Check/call values of public variables from another subroutine in another module[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

In my add-in, I've initialised a couple of checks and log writing (e.g. OS version, Excel version, library associations, availability of add-in files, VBE version, etc.) when Excel opens and before the user actually starts working with the add-in (button clicking, userforms, etc.). The idea is not to bother users with error messages, if smth is missing or they intend to use Excel for other needs. Instead I'd like to have the results of initial checks reserved in public variables (?) so that the user is warned and necessary error handling is carried out only when the user actually starts working with my add-in (e.g. running another subroutine such as a userform after initialisation is complete).

How can I achieve this without storing public variable values in Excel sheet or a separate file? Is it somehow possible given the limitations of Excel VBA?

Thanks!
 
Last edited:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top