+ Reply to Thread
Results 1 to 3 of 3

VBA - Memorizing values of variables

  1. #1
    Registered User
    Join Date
    03-20-2009
    Location
    Lithuania
    MS-Off Ver
    Excel 2013
    Posts
    99

    VBA - Memorizing values of variables

    Hi everybody,

    In my VBA code, I have set that when the excel is opened (Private sub Workbook_open), VBA code changes some values in a few cells. However, I want that before closing excel (Private sub Workbook_BeforeClose(Cancel As Boolean)) values in those cells would be changed back to their original values. I tried to use global variable (Public Back as Boolean). When excel is opened the value of this variable would be set to True if VBA code had to change some values in excel on opening the file. And before closing workbook, VBA code would check if the value of variable Back is equal to True - in that case VBA code would change values to their original. But it appears that after VBA code in Private sub Workbook_open is run excel doesn't memorize the value of variable Back although it is a global variable.

    Does anyone know how to make VBA code memorize values?
    Last edited by walduxas; 01-05-2012 at 08:27 AM.

  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,258

    Re: VBA - Memorizing values of variables

    Hello walduxas,

    Global VBA variables retain their values only for as long as VBA is running. To retain values between openings of a workbook, you would need to save the variables to a file or to a worksheet in the workbook. The easiest method is to add a worksheet and make it "very hidden". This will hide the sheet the user and only be accesible through VBA code. You store the values on this sheet before closing and restore the values when the workbook is reopened.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    03-20-2009
    Location
    Lithuania
    MS-Off Ver
    Excel 2013
    Posts
    99

    Re: VBA - Memorizing values of variables

    Thanks, that's an idea

+ 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