Exception HRESULT: 0x800A03EC when inserting an Ex

2019-09-20 01:40发布

This is the exception I'm getting:

System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC

at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Microsoft.Office.Interop.Excel.Range.set_Formula(Object value)

My code looks like this:

Range rng = activeWorksheet.get_Range("A1");
rng.Formula = "=SUM(A4*C4;A5*C5;A6*C6;A7*C7)/SUM(A4:A7)";

Whenever I run this code I get the previously mentioned exception. However, when I run this code:

Range rng = activeWorksheet.get_Range("A1");
rng.Formula = "=SUM(A4:A7)/4"

This works perfectly. No exception.

I've checked both formulas, they work perfectly fine in my Excel. I've tried setting:

Application.Calculation = XlCalculation.xlCalculationAutomatic;

This does not help at all, I've been googling this solution and have not found anything useful. Does anyone have a clue what might be wrong?

标签: c# excel vba
2条回答
冷血范
2楼-- · 2019-09-20 02:12

You need to update your formula to an array formula using the FormulaArray property of the range object, using { } and an R1C1 reference.

"=SUM(R4C1*R4C3;R5C1*R5C3;R6C1*R6C3;R7C1*R7C3)/SUM(R4C1:R7C1)"

查看更多
Animai°情兽
3楼-- · 2019-09-20 02:35

Excel's COM interface speaks American so you need to use the US list separators in the formula strings. replace your semicolons with commas and you should be fine.

Range rng = activeWorksheet.get_Range("A1");
rng.FormulaArray = "=SUM(A4*C4,A5*C5,A6*C6,A7*C7)/SUM(A4:A7)";
查看更多
登录 后发表回答