+ Reply to Thread
Results 1 to 11 of 11

Setting a Global Variable

  1. #1
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Setting a Global Variable

    If I want to set a global variable when I open my workbook which will be used in code on the individual spreadsheets, how would I do this? I want to set the time the workbook is opened to a variable (constant) and then compare that time to current time on each calculation in the worksheets.

    I'm using Excel 2000.

    Thanks

    ChemistB

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello ChemistB,

    You can do this 2 ways. Declare your variable in the Declarations section of either ThisWorkbook or in any standard VBA module in your project.

    Sincerely,
    Leith Ross

  3. #3
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good evening ChemistB

    Easiest thing is to show you an example.
    Open the attached file and allow macros to run.
    An event procedure will fire straight away and will show the current time (hh:mm:ss).
    Next click on Button1, which is a standard macro, which will show the same value (carried across from the ThisWorkbook.
    Now click on Button2 which will update the variable (I've used "a" dimmed as Date) to the time it is now and display that.

    HTH

    DominicB
    Attached Files Attached Files
    Please familiarise yourself with the rules before posting. You can find them here.

  4. #4
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582
    I have this (simplified) in my workbook code
    Please Login or Register  to view this content.
    and this in my worksheet code
    Please Login or Register  to view this content.
    But when I step through my worksheet code, Starttime is empty.

    ChemistB
    Last edited by ChemistB; 05-27-2008 at 05:10 PM.

  5. #5
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Hi ChemistB

    Take a look at the workbook I uploaded.
    The global variable must be outside the normal routine.

    Please Login or Register  to view this content.
    HTH

    DominicB

  6. #6
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582
    Thanks for your help. I got it now.

    ChemistB

  7. #7
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582
    Alas, it's still not working. I have set "Startdate" as a public variable in both the workbook and worksheet (where I have code) and various combinations. Why isn't "Startdate" keeping it's value?

    If it "protects" on you, the password is calculate.

    Thanks in advance!

    ChemistB
    Attached Files Attached Files

  8. #8
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Hi ChemistB

    From the VBE go to Insert > Module.
    Paste this line into the empty module.
    Please Login or Register  to view this content.
    Delete this line from your ThisWorkbook module and your Sheet1 module.

    HTH

    DominicB

  9. #9
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582
    Interesting, so Excel looks at the modules first and foremost even when you're not running any macros?

    Thanks for your help, Dominic!

    ChemistB

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481
    so Excel looks at the modules first and foremost
    I don't think this is strictly true.

    The scope of the variable in relation to the codes location is what is used.

    Your example had the variable Startdate declared in 2 locations. Thisworkbook and Sheet1.

    So the routine Workbook_Open() in Thisworkbook used the variable declared at the top of the Thisworkbook module.

    In Sheet1 the variable declared at the top of Sheet1 code module was used in the Worksheet_Change event.

    Both of the following would have been valid syntax and returned the value contained by each variable.

    Thisworkbook.StartDate
    Sheet1.StartDate

    If you modified Sheet1 code to and change the sheet first time will get protected and after removing protection the code will work as intended.
    Please Login or Register  to view this content.

    If you add this code to a standard code module.
    Run before changing sheet1 and you can see the held values of both variables.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Cheers
    Andy
    www.andypope.info

  11. #11
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582
    Thanks Andy! I'm learning, bit by bit.

    ChemistB

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1