[英]Range.FormulaArray causing error in specific cases
I have a piece of code which puts an array formula in a range.我有一段代码将数组公式放在一个范围内。 It is throwing runtime error:1004 "Unable to set the FormulaArray property of the Range class".
它抛出运行时错误:1004“无法设置 Range 类的 FormulaArray 属性”。 But when I paste the same formula in the cell and hit Ctrl+Shift+Enter everything works fine.
但是当我在单元格中粘贴相同的公式并按 Ctrl+Shift+Enter 时,一切正常。
strFormula = "=IF(SUM(--(A2=Data!$A$2:$A$1423)*--(B2=YEAR(Data!$B$2:$B$1423))* --(C2=MONTH(Data!$B$2:$B$1423)) * (Data!$E$2:$E$1423))=0,TEXT(,),SUM(--(A2=Data!$A$2:$A$1423)*--(B2=YEAR(Data!$B$2:$B$1423))* --(C2=MONTH(Data!$B$2:$B$1423)) * (Data!$E$2:$E$1423)))"
shtAbsoluteData.Range("D2").FormulaArray = strFormula
The problem occurs when the IF condition was inserted.插入 IF 条件时会出现问题。 So without the IF the following code works fine:
因此,如果没有 IF,以下代码可以正常工作:
strFormula = "=SUM(--(A2=Data!$A$2:$A$1423)*--(B2=YEAR(Data!$B$2:$B$1423))* --(C2=MONTH(Data!$B$2:$B$1423)) * (Data!$E$2:$E$1423))"
shtAbsoluteData.Range("D2").FormulaArray = strFormula
Note: If I use shtAbsoluteData.Range("D2").Formula
then there is no error but the result is incorrect注意:如果我使用
shtAbsoluteData.Range("D2").Formula
那么没有错误但结果不正确
A Range.FormulaArray property can only have 255 characters and yours was showing 248. A few mistypes might have brought it over the limit. Range.FormulaArray 属性只能有 255 个字符,而您的显示为 248 个。一些错误类型可能导致它超过限制。
strFormula = "=IF(SUM((A2=Data!$A$2:$A$1423)*" & _
"(B2=YEAR(Data!$B$2:$B$1423))*" & _
"(C2=MONTH(Data!$B$2:$B$1423))*" & _
"(Data!$E$2:$E$1423))," & _
"SUM((A2=Data!$A$2:$A$1423)*" & _
"(B2=YEAR(Data!$B$2:$B$1423))*" & _
"(C2=MONTH(Data!$B$2:$B$1423))*" & _
"(Data!$E$2:$E$1423))," & _
"TEXT(,))" '<~~ 226 characters
shtAbsoluteData.Range("D2").FormulaArray = strFormula
As I mentioned in comments, the double-unary operator is not necessary when multiplying boolean conditional statements against each other.正如我在评论中提到的,当布尔条件语句相互乘以时, 双一元运算符不是必需的。 The act of the mathematical operation makes the boolean ► numerical conversion.
数学运算的行为使布尔值 ► 数值转换。
In Excel Options, Advanced, Display options for this worksheet you can turn off Show a zero in cells that have zero value.在此工作表的 Excel 选项、高级、显示选项中,您可以关闭在具有零值的单元格中显示零。
Any of the Accounting number formats will display a hyphen in place of a zero value.任何会计编号格式都将显示一个连字符代替零值。
A custom Number Format Code can be generated that simply does not display zeroes at all.可以生成完全不显示零的自定义数字格式代码。 One of the simplest would be,
最简单的方法之一是,
General;General;;@
I got the same problem.我遇到了同样的问题。 What I did to solve the problem is to first declare the range object, then use .FormulaArray on the range.
我为解决问题所做的是首先声明范围对象,然后在范围上使用 .FormulaArray 。
Using C#:使用 C#:
Microsoft.Office.Interop.Excel.Range eRange = wks.Range[wks.Cells[1,1], wks.Cells[20, 20]];
eRange.FormulaArray = $@"=some formula";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.