+ Reply to Thread
Results 1 to 6 of 6

VBA recalculates formulas even when stepping through unrelated code?

  1. #1
    Forum Contributor
    Join Date
    03-13-2009
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    136

    VBA recalculates formulas even when stepping through unrelated code?

    I have a program that uses the following line:

    Please Login or Register  to view this content.
    When I step through the entire program and watch this:

    WorksheetFunction.NormInv(Rnd(), Mu / 100, StDev / 100)))

    I notice that it generates a new random number and recalculates NormInv on each step. I may be wrong but I believe the calculation of NormInv is done through an iterative process which I am guessing eats up a lot of clock cycles. The only time that I need the program to generate a new random number and calculate NormInv is when the program gets to that line. How can I get NormInv to NOT calculate a new number every step through the program?

  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 recalculates formulas even when stepping through unrelated code?

    Hello jerseyguy1996,

    What you're describing is most likely caused by a volatile function. Microsoft lists these worksheet functions as volatile: NOW(), TODAY(), RAND(), OFFSET, INDIRECT, INFO (depending on its arguments), and CELL (depending on its arguments). However, I am uncertain if invoking a worksheet function from VBA will make it volatile. I do know that a UDF (User Defined Function) is non volatile unless the Application.Volatile statement has been added to it.
    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
    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: VBA recalculates formulas even when stepping through unrelated code?

    Use a variable to store a new random number as needed, and then use the variable in lieu of Rnd as long as you want to repeat the use of the same number.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Contributor
    Join Date
    03-13-2009
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    136

    Re: VBA recalculates formulas even when stepping through unrelated code?

    I tried to modify my code by creating a function that uses rnd() to store a random number in a variable but I must be missing something because as rnd() changes, the value of my variable changes with it. How do I get the variable to update its value only when the function is called?

    Here is the code:

    Please Login or Register  to view this content.

  5. #5
    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: VBA recalculates formulas even when stepping through unrelated code?

    What you're doing is no different than twhat you were doing before, only more complicated
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    03-13-2009
    Location
    New Jersey
    MS-Off Ver
    Excel 2007
    Posts
    136

    Re: VBA recalculates formulas even when stepping through unrelated code?

    That did it! Thanks for the help!
    Last edited by shg; 11-10-2009 at 11:13 AM. Reason: deleted spurious quote

+ 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