+ Reply to Thread
Results 1 to 8 of 8

Using a Global Variable in a formula, in a cell, in a Worksheet

  1. #1
    Forum Contributor
    Join Date
    02-01-2019
    Location
    Illinois
    MS-Off Ver
    2016
    Posts
    223

    Using a Global Variable in a formula, in a cell, in a Worksheet

    Is there a way I can use the value of a global variable, e.g., the global variable Chapter
    Please Login or Register  to view this content.
    in a formula that appears in one of the cells of one of my worksheets? I can access the value of these variables in any procedure or function in any worksheet, but I don't see how to access the variables' values in my formulas in cells.


    I currently have working VBA code that assigns a value to the cell of a worksheet. As long as I fully reference the cell, any formulas in any spreadsheet can read the value of a cell that was assigned its value by a VBA assignment.
    The problem with this implementation is remembering what the various cells' meanings are. If I could replace storing a value from VBA in a cell (and using the cell's value in the cell in formulas) with storing values in a variable in VBA (and using the variable's value in formulas in cells), I could name the variables in a way that makes
    my code much more mnemonic and much easier to read.

  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: Using a Global Variable in a formula, in a cell, in a Worksheet

    Named (constant) formula:

    Please Login or Register  to view this content.
    A
    B
    1
    1
    A1: =Chapter
    Entia non sunt multiplicanda sine necessitate

  3. #3
    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: Using a Global Variable in a formula, in a cell, in a Worksheet

    Better example:

    Please Login or Register  to view this content.
    A
    B
    1
    Book The First: Recalled to Life A1: =Chapter


    Be aware that changing the variable won't update the named formula; you would need to change it as the variable changes.

  4. #4
    Forum Contributor
    Join Date
    02-01-2019
    Location
    Illinois
    MS-Off Ver
    2016
    Posts
    223

    Re: Using a Global Variable in a formula, in a cell, in a Worksheet

    OK . . . so rather than tell me how to use a public variable from VBA in the formula within a cell, you're telling me a way to provide mnemonic names to the cells in which I'm currently storing values. That does achieve my desired goal of making the code more mnemonic and easier to read, so I like where you're going.

    I have three questions:
    1) Do I have to repeat the command in every module where I use the public variable, or can I do this just once for all modules in all worksheets?
    2) What would a fully-referenced version of the command look like, or does the above command have to be in a module (as opposed to a worksheet)?
    3) Re "changing the variable won't update the named formula; you would need to change it as the variable changes": I'm guessing you mean the VBA code needs to assign a new value to the public variable, and the new value will appear in the cell of a worksheet. I'm also guessing that, assuming the worksheet is set to automatically recalculate, that the new value in the public variable will be used in any formulas that reference the cell. Is this all correct?
    Last edited by lovecolorado; 03-18-2019 at 02:31 PM.

  5. #5
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: Using a Global Variable in a formula, in a cell, in a Worksheet

    Developing the shg thread/idea (see attachment, maybe you can find some answer here):
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    02-01-2019
    Location
    Illinois
    MS-Off Ver
    2016
    Posts
    223

    Re: Using a Global Variable in a formula, in a cell, in a Worksheet

    I thought I 'had it', but something is still wrong.

    I inserted the code
    Please Login or Register  to view this content.
    and I confirmed the value of the variable named 'Chapter' was now available in any worksheet - which is exactly what I originally wanted. Super!

    So, I generalized the solution to the following
    Please Login or Register  to view this content.
    When I try to run this code, the debugger stops execution and highlights the line
    Please Login or Register  to view this content.
    in yellow. Does anyone have an idea why this would occur?
    Last edited by lovecolorado; 03-19-2019 at 08:27 AM.

  7. #7
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Using a Global Variable in a formula, in a cell, in a Worksheet

    Why don't you just create a function that returns the value of the variable?
    Rory

  8. #8
    Forum Contributor
    Join Date
    02-01-2019
    Location
    Illinois
    MS-Off Ver
    2016
    Posts
    223

    Re: Using a Global Variable in a formula, in a cell, in a Worksheet

    Nice option. I can see how that would work.

    Aesthetically, I would prefer to get the global variables working properly; however, if I can't get that to work, you've given me a good work-around.

+ 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. [SOLVED] Global variable declared in Userform. Variable value is not sticking
    By timtim89 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-25-2012, 08:50 AM
  2. A variable which is used by several subs in a worksheet but is not global?
    By lottesfog in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-08-2008, 06:05 PM
  3. Replies: 3
    Last Post: 09-06-2005, 12:05 PM
  4. Replies: 0
    Last Post: 09-06-2005, 10:05 AM
  5. Replies: 3
    Last Post: 09-06-2005, 07:05 AM
  6. Replies: 0
    Last Post: 09-06-2005, 01:05 AM
  7. Replies: 1
    Last Post: 07-08-2005, 11:05 PM
  8. Replies: 1
    Last Post: 07-08-2005, 09:05 PM

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