坚持克服FormulaArray中的255个字符

时间:2016-03-04 22:22:36

标签: vba excel-vba excel

我花了很长时间试图解决这个问题!如果有人可以第二眼看到我的错误在哪里,我将不胜感激!该公式似乎没有替换

这是我的代码

Sub remove()
Dim theFormulaPart1 As String
Dim theFormulaPart2 As String

theFormulaPart1 = "=IFERROR(INDEX(Curve!D:D,MATCH(1,(DATE(RIGHT(Census!$BY2,4),LEFT(Census!$BY2,2),MID(Census!$BY2,4,2))-DATE(RIGHT(Census!$BM2,4),LEFT(Census!$BM2,2),MID(Census!$BM2,4,2))=Curve!$A:$A)*(Census!$T2=Curve!$C:$C),0)),""X()"")"
theFormulaPart2 = "IFERROR(INDEX(Curve!D:D,MATCH(1,(DATE(Year(Census!$BY2),Month(Census!$BY2),Day(Census!$BY2))-DATE(Year(Census!$BM2),Month(Census!$BM2),Day(Census!$BM2))=Curve!$A:$A)*(Census!$T2=Curve!$C:$C),0)),"""")"

With ActiveSheet.Range("CD2")
.FormulaArray = theFormulaPart1
.Replace """X()"")", theFormulaPart2
End With

2 个答案:

答案 0 :(得分:0)

好的,谢谢@Dirk,结果是255个字符而不是问题

第二眼:

  • 公式太长,吐出来使其在将来可读
  • 使用直接处理字符串的Replace函数
  • 如果只有一个单元格
  • ,则无需使用FormulaArray
  • 替换字符串应为"X()"而不是"X()")

以下是第二眼代码:

Sub remove()
    Dim theFormulaPart1 As String
    Dim theFormulaPart2 As String

    theFormulaPart1 = "=IFERROR(INDEX(Curve!D:D,MATCH(1,(DATE(RIGHT(Census!$BY2,4),LEFT(Census!$BY2,2),MID(Census!$BY2,4,2))-DATE(RIGHT(Census!$BM2,4),LEFT(Census!$BM2,2),MID(Census!$BM2,4,2))=Curve!$A:$A)*(Census!$T2=Curve!$C:$C),0)),""X()"")"
    theFormulaPart2 = "IFERROR(INDEX(Curve!D:D,MATCH(1,(DATE(Year(Census!$BY2),Month(Census!$BY2),Day(Census!$BY2))-DATE(Year(Census!$BM2),Month(Census!$BM2),Day(Census!$BM2))=Curve!$A:$A)*(Census!$T2=Curve!$C:$C),0)),"""")"


    theFormulaPart1 = Replace(theFormulaPart1, """X()""", theFormulaPart2)

    ActiveSheet.Range("CD2") = theFormulaPart1

End Sub

另一个提示:

  • 避免使用ActiveSheet, how?

答案 1 :(得分:0)

错误很简单:

.Replace """X()"")", theFormulaPart2

无效后将无法运行公式。你需要:

.Replace """X()""", theFormulaPart2