View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Harlan Grove
 
Posts: n/a
Default Preventing a function being called if argument is unchanged

Clive wrote...
I have a function in a cell triggered on the value returned by a DDE
link in another cell.

e.g. cell A1 contains =function1(A2) and cell A2 contains
=ADVFN|NYSE_CAT!CUR

function1 is triggered each time the DDE link updates regardless of
whether the result returned has changed or not. How do I prevent
function1 being triggered if the returned value has not changed?


If function1 is a user-defined function which you could modify, then
you could use an approach like


Function function1(arg As Variant) As Variant
Static oldarg As Variant, oldretval As Variant
'
'original variable declarations here
'
If not IsEmpty(oldretval) And oldarg = arg Then
function1 = oldretval
Exit Function
End If
'
'original code here
'
oldarg = x
oldretval = function1
End Function


Excel would still call the function each time A2 refreshes, but it'd
return as quickly as possible. This is the only way to avoid calling
the function without storing the previous value of A2 in a different
cell.