I have a problem with the order in which Excel calls VBA functions
when updating cells in a sheet. Here is the essence of the problem.
Suppose you have three functions fnA fnB and fnC (see below), each takes
a Range argument and returns a String "A", "B", and "C" respectively.
Suppose you put fnA(A1) in A2, fnB(A2) in A3 and fnC(A3) in A4
and then type in something to A1. The functions are called but the order
is unpredictable. It seems to depend on the order I typed things in.
Moreover, if I then rearrange the links, you do not get the order of
updates that you would expect from the order of linkage.
Here is some code to show this. You use the VBA Debug window to see the
execution order.
'*******************************************************************
Private Function fnA(r As Range) As String
Debug.Print "exec: a"
fnA = "A"
End Function
'*******************************************************************
Private Function fnB(r As Range) As String
Debug.Print "exec: b"
fnB = "B"
End Function
'*******************************************************************
Private Function fnC(r As Range) As String
Debug.Print "exec: c"
fnC = "C"
End Function
Now, the interesting thing is that if you modify these functions
so that the argument is a type like double or String, then Excel
finds the correct order. For example these functions will always update
correctly:
'*******************************************************************
Private Function fnA(r As Double) As Double
Debug.Print "exec: a"
fnA = 1
End Function
'*******************************************************************
Private Function fnB(r As Double) As Double
Debug.Print "exec: b"
fnB = 2
End Function
'*******************************************************************
Private Function fnC(r As Double) As Double
Debug.Print "exec: c"
fnC = 3
End Function
My questions are:
What are the rules for Excel updating when VDA functions have Range
parameters (and why are these rules not the same as when the arguments
are Double or String)?
Could I rewrite my first set of VBA functions so that they will update
correctly, given that I must use the same Range argument in the function
calls and return a String that doesn't change?
Oh, I have an old version of Excel (Office 97), but I don't think this
is part of the problem (running under XP).
Bookmarks