+ Reply to Thread
Results 1 to 7 of 7

how to create a "persistent" variable definition?

  1. #1
    Forum Contributor
    Join Date
    09-28-2007
    Location
    New York, NY
    MS-Off Ver
    2007
    Posts
    120

    how to create a "persistent" variable definition?

    Is it possible to create a "dependent" variable that is recalculated automatically when its "independent" variables are changed. For example, can I somehow define y = 3*x +5 such that y is instantly updated when x changes somewhere later in the code? As of now, I would have to re-write the "y=3*x+5" line of code after every time x *might* change.

    I tried searching for this but I have no idea what to search for exactly.

    Thanks very much!
    Last edited by MCCCLXXXV; 12-04-2013 at 07:28 AM.

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: how to create a "persistent" variable definition?

    If you declare Y as a variable why would you have to rewrite the equation?
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Forum Contributor
    Join Date
    09-28-2007
    Location
    New York, NY
    MS-Off Ver
    2007
    Posts
    120

    Re: how to create a "persistent" variable definition?

    in my code, I have x changing in several locations (within several different "if" statements). so, if x does change at some point, I'd also like y to change at the same time, but it seems messy to rewrite "y=3*x+5" after every time x might change.

  4. #4
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: how to create a "persistent" variable definition?

    You can make it a function
    Please Login or Register  to view this content.
    Then use it like this
    Please Login or Register  to view this content.
    after x changes.

  5. #5
    Forum Contributor
    Join Date
    09-28-2007
    Location
    New York, NY
    MS-Off Ver
    2007
    Posts
    120

    Re: how to create a "persistent" variable definition?

    True, but that still requires me to "recalculate" the function after every time x changes. I just thought there was some more elegant way of writing this ... maybe by using pointers or byref somehow. I guess its kind of like I want to change the value of 2 variables at the same time (x and y) with one line of code that only operates on x.

    Thanks very much for your help!

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,399

    Re: how to create a "persistent" variable definition?

    I don't believe there is any way to do this without specifically telling VBA to execute the y=... statement.

    The example you give is such a simple expression, I don't know why it seems undesirable to simply include the y=... statement after every x=... statement. This is probably how I would do it.

    A couple of thoughts that might spark some additional thinking.

    1) Instead of defining x as a value (long or double or whatever), could you define x and y as ranges referring to specific cells in Excel? It is not the kind of programming I do, but I think that you can then have Excel's calculate event perform the y calculation for you. A quick test procedure:
    Please Login or Register  to view this content.
    In my test this created a correct list of y values, even though I did not specifically tell it to execute the y=... statement. This is because Excel was watching for the change to x and would auto calculate y every time it changed x. An approach like this will mean that you will have to make sure that nothing in your VBA code would interfere with Excel's calculation event.

    2) In many ways what I think you are describing is kind of like a subroutine. I don't know if experienced programmers still use Gosub...Return statements (http://msdn.microsoft.com/EN-US/libr.../gg251648.aspx), but that might be another way to do it. Define a subroutine for changing x and y, then your main program flow can call the subroutine everytime you want to change x (and y).
    Please Login or Register  to view this content.
    3) I think many experienced programmers might prefer to use individual sub procedures rather than having the subroutine defined like in 2. This requires thinking about how you will transfer variables from one procedure to another. Here's a possibility using procedure level variables:
    Please Login or Register  to view this content.
    The first effort relies on Excel and its calculate event to "magically" calculate y evertime x changes. The other two approaches do require VBA to explicitly tell the computer to calculate y, but we have structured the code so that changing x and calculating y occur together.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  7. #7
    Forum Contributor
    Join Date
    09-28-2007
    Location
    New York, NY
    MS-Off Ver
    2007
    Posts
    120

    Re: how to create a "persistent" variable definition?

    Thanks very much. I though this was the case, but just wanted to be sure there wasn't a more elegant way of doing it. My actual calculation is much more involved than the example I provided, which I why I wanted a cleaner solution, but a subroutine is what I'll end up using. Thanks again.

+ 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 Object Browser "View Definition" icon is grayed out
    By RonByrd in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 1
    Last Post: 05-14-2013, 04:18 AM
  2. Using VBA to create a variable "adjustable cells" array in Solver
    By Kybynn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-04-2012, 12:25 AM
  3. [SOLVED] create links to check boxes marked "good" fair"and "bad"
    By pjb in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-19-2006, 09:25 PM
  4. [SOLVED] use variable in Workbooks("book1").Worksheets("sheet1").Range("a1"
    By Luc in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-28-2005, 04:05 PM
  5. [SOLVED] VBA code to create "variable" worksheets within a workbook
    By Chris in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-13-2005, 10: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