Hi there.
This is a bit long but it's not that complicated (really) and I've been struggling with it for more than a week now (though I'm not that experienced in Excel or VBA but am an experienced developer otherwise). I've built a relatively simple workbook but am having a confounding problem with formula dependencies (precedents) across sheets. It appears to be an Excel bug (or a serious misunderstanding on my part) but it's not clear yet.
I have 12 sheets named "Jan" to "Dec" in left-to-right order. Each sheet appears as follows (showing "Apr" for this example):
The value 3 above is a just a count of all columns to the left of the "Totals" column (column X) containing the letter 's'. The value 17 in brackets is then just a running total for that row from all previous sheets ("Jan" to "Mar"), including the above sheet itself ("Apr"). The formula I use in the "Totals" column is this:Please Login or Register to view this content.
=MyCountIf(A1:W1, "s", Mar!X1)
The first arg is the range to search for occurrences of 's' on the current row, where 's' is passed via the second arg, and the last arg is the address of the corresponding "Totals" cell on the *previous* sheet ("Mar" in this case). The function simply counts the occurrences of 's' on this row (A1:W1 passed via the first arg), yielding the count for April (3 as seen), and then strips the value between the brackets from the "Totals" cell on the *previous* sheet (cell Mar!X1, not shown above but containing, say, "5 (14)" for this example), resulting in 14. It then adds 3 + 14 = 17 and returns this as "3 (17)", populating the above sheet ("Apr") with this.
For this technique to work however, Excel *must* evaluate each formula in order from Jan to Dec. It should AFAIK since formulas are updated based on changes to their dependent ("precedent") args (from what I've read but it only makes sense anyway). Therefore, since the above formula for "Dec" is dependent on "Nov" which is dependent on "Oct" and so forth all the way back to "Jan" (based on the 3rd arg to my function), if I add or delete an "s" on this row for any month (sheet), the value in the "Totals" column (column X) should be updated on each subsequent sheet and in month order (from left-to-right starting with the sheet I update).
Unfortunately, this doesn't seem to be case. Sometimes it does (do it in month order), but mostly it doesn't. The order is random and inconsistent. My function is very simple though, with no obvious errors, and shouldn't normally impact the dependency order that Excel uses to call it anyway (which I don't control of course). It should *always* be called in month order AFAIK since each month's formula is dependent on the "Totals" cell of the previous month (again, due to the 3rd arg)
The upshot is why might this be occurring. Shouldn't the dependency order Excel uses (given the 3rd arg I'm passing) always cause it to update the "Totals" column on each sheet in month (left-to-right) order?
Lastly, note that I've tried many things including rebuilding the workbook itself but to no avail. I've also tried combining the args A1:W1 and Mar!X1 into a single arg by bracketing them and relying on "Range.Areas" in my function. It appears this is illegal however, likely because they're targeting two different sheets (can anyone confirm this since targeting a single sheet does work). I'm using the latest version of Excel at this writing (16.X).
Thanks for your help (appreciated)
Bookmarks