【问题标题】:Entering a long array formula in Excel VBA using the Replace method as a workaround使用 Replace 方法在 Excel VBA 中输入长数组公式作为解决方法
【发布时间】:2015-09-29 15:29:59
【问题描述】:

在通过 VBA 输入数组公式时,我试图避免 255 个字符的限制。我在网上查了一下,发现了一个很有前途的解决方案,可以将数组分成几部分并再次组合。

但是,我的 .replace 函数没有替换任何东西。

我非常小心地确保在 .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

每次运行宏时,单元格中的公式都保持不变。单步执行代码不会引发错误或提示出错的地方。只是,什么都没有发生。

【问题讨论】:

    标签: arrays vba excel


    【解决方案1】:

    两个替换字符串都没有正确定义。您应该在字符串常量中使用""""" 而不是""

    第二个问题 - 在theFormulaPart3 末尾缺少))

    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)))"
    

    【讨论】:

      猜你喜欢
      • 2015-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多