+ Reply to Thread
Results 1 to 10 of 10

Storing a value in a variable for use at a later date.

  1. #1
    Registered User
    Join Date
    11-05-2007
    Posts
    82

    Storing a value in a variable for use at a later date.

    Hello again,

    I have an Add-in that creates and formats reports for various users.

    Within the add-in I have rules set to disable some buttons on a userform untill a process is run to enable them. I am doing this through the use of a variable - set as "0" untill such a time when the process is run that it will set the variable to "1". A function is then run to enable all of the buttons.

    However, once excel is shutdown and re-openned - it resets the variable to "0" for no apparent reason. Is this normal? Is there a way around this - so that the Add-in remembers what the variable was set to before closure?

    Appreciate your help! Neil

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    The life of a variable is dependant on where it is declared. But regardles of where they are declared they can not survive the closing of Excel

    have a look at
    http://www.ozgrid.com/VBA/variable-scope-lifetime.htm

    You will need to store the settings before you close the workbook
    This could be with the workbook, a text file, a registry entry
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    All variables are lost when Excel closes. You can store the value in a Text file, the registry or more simply in the workbook
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Registered User
    Join Date
    11-05-2007
    Posts
    82
    I thought that was going to be the case!

    As its an add-in I dont have a workbook open, so wouold probably go with the text file to store the variable, but not sure how I would do this - and how I would pick this back up when re-openning the add -in?

    Thanks

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    An addin has a WorkBook_Open event, and a sheets in which you can store the value(s).

  6. #6
    Registered User
    Join Date
    11-05-2007
    Posts
    82
    The add-ins are going to be installed on individuals PC's though - and I want the indicater recognised by all.

    So I think the text file would be best - but not sure how you would reference a text file to store and to re-establish once open?

    Thanks

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Something like this
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    11-05-2007
    Posts
    82
    Excellent - thanks for your help.

  9. #9
    Registered User
    Join Date
    11-05-2007
    Posts
    82
    Just out of curiousity - how does the Print #fnum part of the code work? This doesn't seem to be set as a variable, yet it still works.

    Tried looking it up in the help - but cant find anything:


  10. #10
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    fnum is a variable and is set by this command
    Please Login or Register  to view this content.
    Freefile command Returns an Integer representing the next file number available

    This command opens the file which is then referenced by the file number
    Please Login or Register  to view this content.
    this prints to that file number
    Please Login or Register  to view this content.

+ 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