excel formulaarray

时间:2009-06-29 20:14:24

标签: excel excel-formula excel-2007 excel-vba vba

为什么我得到运行时错误13:输入错配错误 在运行以下代码时

 Application.Goto Reference:="R1C1:R232C221"
 Selection.FormulaArray = "=ROUND(a(),0)"
 Selection.Replace What:="a()", Replacement:="IF(IF(Sheet4!A1:HM232+Sheet5!A1:HM232=2,0," & _
"Sheet4!A1:HM232+Sheet5!A1:HM232)+IF(Sheet4!A1:HM232+Sheet5!A1:HM232=2,0," & _
"Sheet4!A1:HM232+Sheet5!A1:HM232)=2,0,IF(Sheet4!A1:HM232+Sheet5!A1:HM232=2,0," & _
"Sheet4!A1:HM232+Sheet5!A1:HM232)+IF(Sheet4!A1:HM232+Sheet5!A1:HM232=2,0,Sheet4!A1:HM232+Sheet5!A1:HM232))", LookAt _
    :=xlPart, SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
    ReplaceFormat:=False
Range("I9").Select

1)我知道公式阵列应该是R1C1风格......但是A1风格不是必需的,它在A1风格中也没有任何问题

  http://msdn.microsoft.com/en-us/library/bb208529.aspx

2)我从

中找到了这种写作方式
  http://www.dailydoseofexcel.com/archives/2005/01/10/entering-long-array-formulas-in-vba/

2 个答案:

答案 0 :(得分:4)

让我们分析一下你的替代品:

Replacement:="IF(IF(Sheet4!A1:HM232+Sheet5!A1:HM232=2,0," & _
"Sheet4!A1:HM232+Sheet5!A1:HM232)+IF(Sheet4!A1:HM232+Sheet5!A1:HM232=2,0," & _
"Sheet4!A1:HM232+Sheet5!A1:HM232)=2,0,IF(Sheet4!A1:HM232+Sheet5!A1:HM232=2,0," & _
"Sheet4!A1:HM232+Sheet5!A1:HM232)+IF(Sheet4!A1:HM232+Sheet5!A1:HM232=2,0,Sheet4!A1:HM232+Sheet5!A1:HM232))"

(1)将Sheet4!A1:HM232+Sheet5!A1:HM232更改为X

Replacement:="IF(IF(X=2,0," & _
"X)+IF(X=2,0," & _
"X)=2,0,IF(X=2,0," & _
"X)+IF(X=2,0,X))"

(2)将破碎的碎片粘在一起:

Replacement:="IF(IF(X=2,0,X)+IF(X=2,0,X)=2,0,IF(X=2,0,X)+IF(X=2,0,X))"

(3)将IF(X=2,0,X)更改为Y

Replacement:="IF(Y+Y=2,0,Y+Y)"

(4)一些评价:

  • 如果X为2,则Y为0,结果为0.
  • 如果X为1,则Y为1,结果为   0
  • 如果X是其他任何东西,Y是X,而且是 结果是2 * X。

(5)所以公式相当于:

Replacement:="IF(OR(X=2,X=1),0,2*X)"

(6)所以下一步是替换X(反向步骤1)...

我会给你留下一两个问题:它是如何变得如此混乱?你听说过“干”(不要重复自己)吗?

答案 1 :(得分:0)

我认为“替代”的论点太长了。您需要将该字符串保持在255个字符以下。将更多公式放入Selection.FormulaArray行,以便替换小于255个字符。