+ Reply to Thread
Results 1 to 10 of 10

Calculation order for UDF's

  1. #1
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Calculation order for UDF's

    I just put together a really slick little UDF that works brilliant. As usual when I enter the VBA world there is a lot of testing and troubleshooting before it works.

    As part of testing I used the debug.print command to see what was going on with various variables. It made no sense at all, the values were all wrong!

    So I tested debug.print c.row where c is one of the input cells to the UDF on the same row.
    I expected to see 2, 3, 4, 5, 6, 7 etc as the UDF was just copied down a column.
    To my surprice the row numbers where all scrambled???

    What is going on here? Does this reveal the true calculation order based on other formulas in the workbook? Still, the order makes no sense.
    <----- If you were helped by my posts you can say "Thank you" by clicking the star symbol down to the left

    If the problem is solved, finish of the thread by clicking SOLVED under Thread Tools
    I don't wish to leave you with no answer, yet I sometimes miss posts. If you feel I forgot you, remind me with a PM or just bump the thread.

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

    Re: Calculation order for UDF's

    I could replicate the behavior with a simple UDF like
    Please Login or Register  to view this content.
    I put =RAND() in column A (rows 1 to 149), and the UDF in column B. I suspect that this is revealing the actual order that Excel is calculating the spreadsheet. I do not claim to understand the internal workings of Excel's calculation engine. I agree, in this scenario, I don't understand why it does not just start at the top and work down. Even if I don't understand the algorithm behind it, I do notice that it seems to use the same calculation order each calculate event. It always started in row 146 (4th from the bottom) and did every 4th row on its way to the top. Then start near the bottom again and work towards the top in a different pattern, and so on. Interestingly, it always calculated row 1 last.

    I thought it might have to do with multi-threading, where different copies of the UDF were being calculated in different threads, but I disabled multi-threaded calculation in Excel Options, and observed no change.

    I suspect it does reveal something about the true order of calculation. I think it shows that Excel does not use a simple top to bottom left to right algorithm when it does not matter, though I cannot say what algorithm Excel is using to decide what order to perform the calculations.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

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

    Re: Calculation order for UDF's

    I decided to try a couple of simple changes to see if I could change the calculation order.

    I tried to force column A to calculate from top to bottom. A1 =RAND(), A2 and down =RAND()+A1. Column B's calculation order still seems random, but it is a different random than the previous test. Again, it is repeatable.

    I tried to force column B to calculate from top to bottom with a similar change. B1 =half(A1,row()), B2 and down =half(A2+B1,row()). This time, the calculation order is in order from 1 to 149.

  4. #4
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Re: Calculation order for UDF's

    Nice to get some feedback on this seemingly boring post! Even nicer that you could replicate it, I didn't expect either.

    So at least this seems to be "normal" behavior by Excel then. Thanks for the feedback!

  5. #5
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Re: Calculation order for UDF's

    Oh, I see. Well for the record my UDF did not contain ROW(), just a reference to a cell on the same row.

  6. #6
    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: Calculation order for UDF's

    AFAIK, you cannot get VBA UDFs to use multiple-threads.
    Rory

  7. #7
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Re: Calculation order for UDF's

    I mean, I didn't use ROW() as input like you do.

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

    Re: Calculation order for UDF's

    So, I changed my test udf to pull the row from the argument
    Please Login or Register  to view this content.
    Now the calculation order seems to be top to bottom. So this changes the order Excel chooses to perform the calculation. Again, it is consistent from calculation event to calculation event.

  9. #9
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Re: Calculation order for UDF's

    Yup, I recognize the patterns. This is what I get now:
    Please Login or Register  to view this content.
    (this has got to be my nerdiest post ever)

  10. #10
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Re: Calculation order for UDF's

    I love this thread!

+ 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. pending order calculation
    By johncena in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-12-2013, 10:26 PM
  2. Help with math bracket calculation order
    By ronlaboa in forum Excel General
    Replies: 3
    Last Post: 12-12-2012, 04:58 AM
  3. Volatile - Calculation Order
    By Zalambur in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-05-2011, 11:35 PM
  4. [SOLVED] Order of calculation
    By Louise in forum Excel General
    Replies: 12
    Last Post: 06-02-2006, 03:55 AM
  5. [SOLVED] Order of calculation
    By RW in forum Excel General
    Replies: 3
    Last Post: 01-10-2006, 10:15 AM
  6. SQL.REQUEST Calculation Order
    By Ryan Smith in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-08-2005, 10:06 PM
  7. Order of Calculation
    By John H W in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-07-2005, 08:06 PM
  8. [SOLVED] BIMDAS - Order of Calculation
    By Atreides in forum Excel General
    Replies: 20
    Last Post: 02-22-2005, 11: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