VBE variables initialized at the start

Thread Starter

atferrari

Joined Jan 6, 2004
4,764
Visual Basic for Excel

I declare my variables: Dim ver_1, ver_2, ver_3 as Variant

I need them initialized to a certain value as soon as I open my application, most probably in Page 1 (but Page 2 could be eventually needed instead).

Stuck on this for the last day, I cannot see how to do it.

Could anyone show the actual code needed?

Gracias for that.
 

ErnieM

Joined Apr 24, 2011
8,377
I don't have excel on this machine to look... but the workbook should have something like a Workbook_Open event. That would be a super good place to put your initialization code.

Did you ask google yet?
 

Thread Starter

atferrari

Joined Jan 6, 2004
4,764
My question was not actually to the point. I know how to initialize them but I cannot get them be visible for the rest of the code (In Sheet1, for example.. I use "Public" ahead but nothing happens.

THanks for replying
 

t_n_k

Joined Mar 6, 2009
5,455
How about adding a module to the VBA project. Declare your public variables in the module. Use Workbook_Open() event to assign the initial values to the public variables. Save [as macro-enabled workbook]and restart the project. The variables as initialized should then be available on any worksheet when the workbook opens.
 
Last edited:

Thread Starter

atferrari

Joined Jan 6, 2004
4,764
How about adding a module to the VBA project. Declare your public variables in the module. Use Workbook_Open() event to assign the initial values to the public variables. Save [as macro-enabled workbook]and restart the project. The variables as initialized should then be available on any worksheet when the workbook opens.
YES! The key was declaring "Public xxx as xxxx" in the module (what did not work in WorkBook).

As you said, the _Open event worked as expected, dutifully initializing the variables.

Gracias.
 
Top