ExcelReference.SetValue with Formula

1,264 views
Skip to first unread message

Fred

unread,
Jul 17, 2009, 8:26:50 AM7/17/09
to ExcelDna
Hi,

I don't find how to set a formula in a cell.
Ex with a ExcelReference if i do xlRef.SetValue("=SUM(A1:A3)"); i show
in the cell the text formula and not the result.

Anyone has a response ?

Govert van Drimmelen

unread,
Jul 17, 2009, 8:40:50 AM7/17/09
to ExcelDna
Hi Fred,

At last! - I have a suggestion for one of your questions:
ExcelReference.SetValue internally calls XlCall.Excel
(XlCall.xlSet, ...) which just sets the cell contents. To set the
formula you need xlcFormula:
Try this:
bool result = (bool)XlCall.Excel(XlCall.xlcFormula, xlRef,
formula);
I think you need to use R1C1 references in your formula.

For array formulae, you use XlCall.xlcFormulaArray.

Let us know if it works.

--Govert--

Fred

unread,
Jul 17, 2009, 8:57:37 AM7/17/09
to ExcelDna
Hi

thanks for your reply.
I have already test it with :
XlCall.Excel(XlCall.xlcFormula, new ExcelReference(5,5), "=TODAY
()");
with or without "=" before the function, but it doesn't work
I have the error "Invalid reference".

Thanks

Govert van Drimmelen

unread,
Jul 17, 2009, 9:33:58 AM7/17/09
to ExcelDna
Hi Fred,

Sorry, the arguments are the other way around. Try:
XlCall.Excel(XlCall.xlcFormula, "=TODAY()", new ExcelReference(5,5) );

In some cases you might like to use
XlCall.xlcFormula | XlCall.xlIntl
which I think allows you to use English formula names in international
versions.

--Govert--

Fred

unread,
Jul 17, 2009, 10:12:57 AM7/17/09
to ExcelDna
Do you have a sample for the FormulaArray use ?

Thanks

Fred

unread,
Jul 17, 2009, 10:07:36 AM7/17/09
to ExcelDna
Thanks it works !

Jack Woodward

unread,
May 16, 2014, 7:11:37 PM5/16/14
to exce...@googlegroups.com
SetValue is extremely fast is there any way to get excel to evaluate the formula after the fact? 

Govert van Drimmelen

unread,
May 17, 2014, 3:53:06 AM5/17/14
to exce...@googlegroups.com
Hi Jack,

ExcelReference.SetValue sets the value, and not the formula of a cell. So it's like putting a ' in front of the formula text in the cell.

To set the formula, you need to use XlCall.xlcFormula. After using this to set the formula, Excel should automatically recalculate the cell (if the book is not in Manual calculation mode.)

Regards,
Govert

Jack Woodward

unread,
May 19, 2014, 12:02:42 PM5/19/14
to exce...@googlegroups.com
Hi Govert,

Is there an overload of xlcFormula that takes a multi-cell ExcelReference and an array of forumlas similar to how xlSet works?  The com api has this functionality through range.Formula so I was thinking there might be a C equivalent.

xlcFormula just puts the array in a single cell.

Thanks,
Jack

Govert van Drimmelen

unread,
May 19, 2014, 3:32:27 PM5/19/14
to exce...@googlegroups.com
Hi Jack,

Nothing that I know of. 
There is xlcFormulaArray to set array formulas, but I don't think that's what you're asking.

Regards,
Govert
Reply all
Reply to author
Forward
0 new messages