+ Reply to Thread
Results 1 to 8 of 8

Retaining a variable value even after macro exits

  1. #1
    Registered User
    Join Date
    05-27-2019
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Office 365
    Posts
    5

    Exclamation Retaining a variable value even after macro exits

    Hello,

    Is there a way of saving a variable in the .xlam file that executes the macro, so that if either Excel is closed, or the macro is applied to various other workbooks, the variable value is retained?

    The user must have the ability to change the variable if needed, and the new value needs to be saved. The value is not used in the sheet but used in calculations in the macro modules and presented on a userform

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Retaining a variable value even after macro exits

    You could store the value in a cell in the xlam file.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    05-27-2019
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Office 365
    Posts
    5

    Re: Retaining a variable value even after macro exits

    Quote Originally Posted by shg View Post
    You could store the value in a cell in the xlam file.
    Do you have a code snippet that you can share to implement this?

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,811

    Re: Retaining a variable value even after macro exits

    Are you familiar with the Range object and how to access cells in VBA? It seems so simple, that it feels like there is more to your question. Something as simple as
    Please Login or Register  to view this content.
    would store the variable's value in cell A1 of the first sheet.

    If you are unfamiliar with how to work with cells/range objects, you might review this document: https://docs.microsoft.com/en-us/off....Range(object)
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Registered User
    Join Date
    05-27-2019
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Office 365
    Posts
    5

    Re: Retaining a variable value even after macro exits

    Ok, so I use the code saved in an .xlam file to link several macro modules from this file to my quick access toolbar. This allows me to perform certain functions on the data entered every time I open new .xlsx files. Will the "Thisworkbook" function access the newly opened .xlsx file or the xlam file that contains the macros? I need to save the variable values in the .xlam file....

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,811

    Re: Retaining a variable value even after macro exits

    The "ThisWorkbook" object specifically refers to the workbook that contains the code. If the code is in an .xlam file, then Thisworkbook refers to the .xlam file containing the code. https://docs.microsoft.com/en-us/off...n.thisworkbook

    the ActiveWorkbook object will refer to the currently active workbook. https://docs.microsoft.com/en-us/off...activeworkbook

    To access other workbooks, you use the Workbooks collection to return the specific workbook you want. https://docs.microsoft.com/en-us/off...tion.workbooks

    If you are new to VBA and Excel, one of the most useful things to learn is Excel's object model. The left frame of each of those help files should contain a "tree" that will allow you to explore Excel's object model.

  7. #7
    Registered User
    Join Date
    05-27-2019
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Office 365
    Posts
    5

    Re: Retaining a variable value even after macro exits

    I have tried the ThisWorkbook option, it works but when I close the .xlsx file and open again, the variable values are not retained ...

  8. #8
    Registered User
    Join Date
    05-27-2019
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Office 365
    Posts
    5

    Re: Retaining a variable value even after macro exits

    My bad, they are saved... thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. VBA that exits a MACRO if clipboard is empty
    By hans302 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-21-2017, 02:56 PM
  2. [SOLVED] Global variable not retaining value
    By Kramxel in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-07-2015, 06:58 AM
  3. [SOLVED] Global Variable is not retaining its value
    By mahmud1280 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-19-2015, 07:32 AM
  4. [SOLVED] Macro to check if sheet name exits
    By robrobet in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-06-2015, 01:00 PM
  5. How do I auto select yes when macro exits a report
    By mattress58 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-16-2014, 12:37 PM
  6. [SOLVED] Retaining Userform Input In A Variable
    By Steve0492 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-15-2013, 01:08 AM
  7. Reprotect after macro exits.
    By D1TrueGod in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-20-2008, 12:34 AM

Tags for this Thread

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