在Excel VBA中使用Replace方法input一个很长的数组公式

我试图通过VBA来避免input数组公式中的255个字符限制。 我已经在网上查找,并find了一个有希望的解决scheme,将数组分解成几部分并再次组合。

但是,我的.replace函数并没有取代任何东西。

我非常小心地确保在每个阶段的结果公式是合乎逻辑的。

这是我到目前为止:

Sub ArrayMacro() Dim theFormulaPart1 As String Dim theFormulaPart2 As String Dim theFormulaPart3 As String theFormulaPart1 = "=IF(SUM(IF(COUNTIFS(Ratting!A:A,'Site Matrix'!$B$1:$FA$1,Ratting!K:K,'Site Matrix'!A2)>=1,1,0))=1,1,1)" theFormulaPart2 = "SUMIF(Ticks!E:E,'Site Matrix'!A2,Ticks!C:C)/IF(COUNTIFS(Ratting!N:N,TRUE,Ratting!K:K,'Site Matrix'!A2)>0,"",1))" theFormulaPart3 = "COUNTIFS(Ratting!A:A,IF(COUNTIFS(Ratting!N:N,TRUE,Ratting!K:K,'Site Matrix'!A2)>0,"",INDEX(Ratting!A:A,MATCH('Site Matrix'!A2,Ratting!K:K,0))),Ratting!K:K,'Site Matrix'!A2)" With Sheets("Site Matrix").Range("FB2") .FormulaArray = theFormulaPart1 .Replace "1,1)", theFormulaPart2, xlPart .Replace "1))", theFormulaPart3, xlPart End With End Sub 

每次运行macros时,单元格中的公式都保持不变。 单步执行代码不会引发错误或提示错误。 只是,没有任何反应。

两个replacestring都没有定义正确。 你应该使用"""""而不是""string常量。

第二个问题 – 缺失))在方法部分3的末尾。

 theFormulaPart2 = "SUMIF(Ticks!E:E,'Site Matrix'!A2,Ticks!C:C)/IF(COUNTIFS(Ratting!N:N,TRUE,Ratting!K:K,'Site Matrix'!A2)>0,"""",1))" theFormulaPart3 = "COUNTIFS(Ratting!A:A,IF(COUNTIFS(Ratting!N:N,TRUE,Ratting!K:K,'Site Matrix'!A2)>0,"""",INDEX(Ratting!A:A,MATCH('Site Matrix'!A2,Ratting!K:K,0))),Ratting!K:K,'Site Matrix'!A2)))"