C# equivalent to Application.Volatile

501 views
Skip to first unread message

SmeeZee

unread,
Sep 20, 2010, 12:43:53 PM9/20/10
to Excel-Dna
Hi there

I have written some functions in C#, which interact with a web service
(using ExcelDNA). However I have noticed these functions will not
update on loading the workbook or pressing F9. Investigating I have
noticed that in VB(A) you would set Application.Volatile.

I have searched for the equivalent for C# but cannot find anything. Am
I on the right track? Could you give me any adivse?

Thanks

Govert van Drimmelen

unread,
Sep 20, 2010, 3:12:05 PM9/20/10
to Excel-Dna
Hi,

You can set a function to volatile in the ExcelFunction attribute:
[ExcelFunction(Description = "Get random number.", IsVolatile
= true)]
public static double GetRandom()
{
return rnd.NextDouble();
}

Or you can explicitly switch volatile on or off in your function with
a call to
XlCall.Excel(XlCall.xlfVolatile, true);


Regards,
Govert

insomniac

unread,
Sep 20, 2010, 4:39:33 PM9/20/10
to Excel-Dna
Hi,

I have a UDF that seems to perform a calculation every time one tabs
from one input field to another in Excel when using the function
wizard (I see the formula result changing at the bottom, and can
replicate this behaviour in debug mode).

Will setting 'IsVolatile = false' address this problem of auto
recalculation?

Thanks.

Govert van Drimmelen

unread,
Sep 20, 2010, 5:06:15 PM9/20/10
to Excel-Dna
Hi,

The recalculation of your function in the function wizard is standard,
and is not affected by the volatile setting at all.

Your function can check whether it is being called from the function
wizard by calling ExcelDnaUtil.IsInFunctionWizard(). A VB example is:

Function SlowFunction(val)
If ExcelDnaUtil.IsInFunctionWizard() Then
SlowFunction = "#IN WIZARD"
Else
SlowFunction = val * 2
End If
End Function

Note that the IsInFunctionWizard() call itself has some overhead
(maybe 1ms), so you should only use it for functions that are quite
slow (take at least a few tens of milliseconds to calculate). For fast
function you are better off having them recalculated in the wizard as
they are inserted, with some parameter validation to deal with the
partial parameters being entered.

Regards,
Govert

Anthony Smee

unread,
Sep 21, 2010, 4:46:23 AM9/21/10
to exce...@googlegroups.com
Thank you! Just what I was looking for.

--
You received this message because you are subscribed to the Google Groups "Excel-Dna" group.
To post to this group, send email to exce...@googlegroups.com.
To unsubscribe from this group, send email to exceldna+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/exceldna?hl=en.


Joe Chan

unread,
Feb 28, 2016, 1:29:22 AM2/28/16
to Excel-DNA
Hi, 

I have been written some functions in C#, which interact with excel using excelDNA.  The function includes a boolean test, which will execute only if the condition is true on a particular excel cell.    This function works perfectly ok when run on a single excel cells.  I try to run the same function in multiple cells in the same column.  When the function in the lower cell is triggered, the same function in the upper cell will be dead.  I mean the function in the upper cell will not be trigger even the condition is met.    I have tried to set the function to volatile but in vain.   Do you have any idea?

Joe 



Govert van Drimmelen於 2010年9月21日星期二 UTC+8上午5時06分15秒寫道:

Govert van Drimmelen

unread,
Feb 28, 2016, 1:41:43 AM2/28/16
to exce...@googlegroups.com
Hi Joe,

Marking your function with 
[ExcelFunction(IsVolatile=true)]
will register your function as volatile, and it will recalculate when the sheet calculates.
Then it works like the built-in RAND() function.

But that might not be exactly what you want.
Does the function take the all inputs it needs to check for its condition, as parameters?
If so, then Excel will automatically recalculate it if the input values change.

Perhaps a simple example of what you want will be easier to discuss.

-Govert
--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.

To post to this group, send email to exce...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages