VBE variables initialized at the start

Discussion in 'Programmer's Corner' started by atferrari, Sep 5, 2013.

  1. atferrari

    Thread Starter AAC Fanatic!

    Jan 6, 2004
    2,648
    762
    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.
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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?
     
  3. atferrari

    Thread Starter AAC Fanatic!

    Jan 6, 2004
    2,648
    762
    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
     
  4. t_n_k

    AAC Fanatic!

    Mar 6, 2009
    5,448
    782
    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: Sep 6, 2013
    atferrari likes this.
  5. atferrari

    Thread Starter AAC Fanatic!

    Jan 6, 2004
    2,648
    762
    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.
     
Loading...