+ Reply to Thread
Results 1 to 4 of 4

how to count iterations from the last calculate event

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

    how to count iterations from the last calculate event

    I put together a simple spreadsheet that uses successive approximations to solve quadratic equations to illustrate what I am trying to do (something I picked up in an analytical chemistry class). I have a few spreadsheets where I use successive approximations in circular references (iteration enabled) to solve some equations, and the spreadsheet works very well. The only thing I wish I had was a way to tell how many iterations were used during the last calculate event to obtain the results.

    When my computer was slower, I could watch the status bar while it counted off iterations, and I could get a rough idea of how many iterations it took, but now, my computer is so fast, I can see the status bar flicker, but I cannot tell if it took 10 or 100 iterations (my usual maxiteration setting). The goal would be to know if Excel terminated calculation because it reached maxiteration or if it determined that the spreadsheet had converged in fewer iterations.

    Does anyone have any ideas for capturing the number of iterations from a calculate event?
    Attached Files Attached Files
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: how to count iterations from the last calculate event

    Maybe something along the lines of:
    Please Login or Register  to view this content.
    With the decimal places in the round function matching your max change.
    Although you would probably want to increment no. iterations by a bigger step and then "hone in".

    I don't believe there is anyway to get the number of iterations taken, although there is an add-in mentioned in this thread:
    http://www.excelforum.com/excel-prog...terations.html

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

    Re: how to count iterations from the last calculate event

    I think I have something functional. In reading the links in the thread mentioned by yudlugar, someone talked about what you see with a UDF. So I tried an approach using a UDF to calculate x(i+1) from x(i). While I could not fully understand everything that was explained at the decisionmodels pages, I saw a possible opportunity, using a UDF, to count iterations.

    You can see that the basic approach was to create a UDF for the x calculation, then call that in a circular reference. Each time the function is called as part of each iteration, it calls the counter procedure that increments i. I'm not sure if it needs to be done this way, but I wasn't sure how to transfer i to the calculate event procedure for recording in the spreadsheet. This way, counter and the calculate event could share the i variable.

    One effect is that i represents the number of times the UDF is called during the calculation event, not necessarily the number of iterations. I divide by the number of instances of the function to get an approximation of the number of iterations. After it has converged (or terminated calculation for max iterations) the calculation event procedure is called to write i to the spreadsheet and reset i.

    This isn't the kind of programming I usually do, so, while it seems functional, I'm not sure exactly how to test it for unintended consequences or if there would be a more robust way to program this. Constructive criticisms would be appreciated.
    Attached Files Attached Files

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

    Re: how to count iterations from the last calculate event

    I have been through a few tests (multiple tabs in a spreadsheet, multiple workbooks open) and it seems to work pretty well. I'm going to mark this as SOLVED, but would still welcome any constructive criticisms to point out things I may be missing, or different ways of accomplishing the same thing -- even if it is only for my own education in doing this kind of programming.

+ 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] change event AND calculate event on the same sheet
    By lauriejerome in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-14-2013, 03:54 PM
  2. Replies: 4
    Last Post: 11-07-2012, 04:02 PM
  3. Replies: 5
    Last Post: 06-23-2005, 06:05 PM
  4. Change event and calculate event
    By Antje in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-29-2005, 04:06 PM
  5. AFTER Calculate Event
    By PosseJohn in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-16-2005, 04:06 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