Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel recalculation order - why dependent cell calculates before preceding cell?

I have a weird issue with Excel calculation order - dependent cell gets calculated before preceding cell with empty parameter value when setting range dirty. Is there any way to detect those "fake" calls? It is critical for me as I run some clever business logic in real world application and those "fake" calls break all logic. In addition those "fake" calls if registered with RTD don't bind it to the topic properly and thus don't notify topic disconnect.

Excel 2010/32bit latest all.

To illustrate problem I have created simple function:

Function Fnc(param2 As String) As String
    Fnc = Application.ThisCell.Address
    Debug.Print (Application.ThisCell.Address & " - " & param2)
End Function

And procedure:

Sub SetDirty()
    Range("B1:C1").Dirty
End Sub

And simple sheet:

$A$1 =fnc(A1) =fnc(B1)

into cells A1, B1, C1.

And result obviously:

$A$1 $B$1 $C$1

If I recalculate (CTRL+Alt+Shift+F9) I get, as expected:

$B$1 - $A$1

$C$1 - $B$1

But, here goes the fun part. If I run SetDirty:

$C$1 - <---- Where does this come from?

$B$1 - $A$1

$C$1 - $B$1

Thanks for any input. I would prefer a solution which solves this issue, not a workaround as it is already part of bunch of workarounds.

like image 746
Arnas Avatar asked May 31 '12 22:05

Arnas


1 Answers

Cells often get recalculated more than once: Excel starts with a recalculation sequence which is the previous calculation sequence plus LIFO (last in first out) for all modified cells. This causes functions to be called and given uncalculated cells - the functions get called again later.
You can detect uncalculated cells in your UDF using IsEmpty
See http://www.decisionmodels.com/calcsecretsj.htm for details on UDFs and detecting uncalculated cells
and http://www.decisionmodels.com/calcsecretsc.htm for details of Excel's calculation sequence.

BTW be careful with Range.Dirty - its a bit buggy

like image 143
Charles Williams Avatar answered Oct 31 '22 01:10

Charles Williams