VBA运行时错误1004:无法设置范围类的formulaarray属性

时间:2016-11-18 14:03:29

标签: excel vba excel-vba

VBA运行时错误1004:无法设置范围类

的formulaarray属性

我在这个link中遵循了Dick Kusleika的建议,但无法通过VBA获得以下数组公式进入excel。谁能看到我哪里出错了?公式的两半都很容易低于255个字符。

Public Sub Configuration()

Dim theFormulaPart1 As String

Dim theFormulaPart2 As String

theFormulaPart1 = "=IF(ISODD(B2),IFERROR(INDEX(Race1Grid,MATCH(C2&I2&""Q3""," & _
                "QualRace1ID&QualDriver&QSession,0)),IFERROR(INDEX(Race1Grid" & _
                ",MATCH(C2&I2&""Q2"",QualRace1ID&QualDriver&QSession,0)),INDEX" & _
                "(Race1Grid,MATCH(C2&I2,QualRace1ID&QualDriver,0))))," & _
                "X_X_X())"

theFormulaPart2 = "IFERROR(INDEX(Race2Grid,MATCH(C2&I2&""Q3"",QualRace2ID&" & _
                "QualDriver&QSession,0)),IFERROR(INDEX(Race2Grid,MATCH(C2&" & _
                "I2&""Q2"",QualRace2ID&QualDriver&QSession,0)),INDEX(" & _
                    "Race2Grid,MATCH(C2&I2,QualRace2ID&QualDriver,0)))))"

    With Worksheets("Races").Range("V2")
        .FormulaArray = theFormulaPart1
        .Replace "X_X_X())", theFormulaPart2
    End With

End Sub

我尝试将公式拆分为更多行,以便于阅读。

1 个答案:

答案 0 :(得分:1)

正如我在评论中提到的,它只是公式第一部分的长度。我建议你进一步简化:

Public Sub Configuration()

Dim theFormulaPart1 As String

Dim theFormulaPart2 As String

Dim theFormulaPart3 As String

theFormulaPart1 = "=IF(ISODD(B2),X_X_X1(),X_X_X())"
theFormulaPart3 = "IFERROR(INDEX(Race1Grid,MATCH(C2&I2&""Q3""," & _
                "QualRace1ID&QualDriver&QSession,0)),IFERROR(INDEX(Race1Grid" & _
                ",MATCH(C2&I2&""Q2"",QualRace1ID&QualDriver&QSession,0)),INDEX" & _
                "(Race1Grid,MATCH(C2&I2,QualRace1ID&QualDriver,0)))),"
theFormulaPart2 = "IFERROR(INDEX(Race2Grid,MATCH(C2&I2&""Q3"",QualRace2ID&" & _
                "QualDriver&QSession,0)),IFERROR(INDEX(Race2Grid,MATCH(C2&" & _
                "I2&""Q2"",QualRace2ID&QualDriver&QSession,0)),INDEX(" & _
                    "Race2Grid,MATCH(C2&I2,QualRace2ID&QualDriver,0)))))"

    With ActiveSheet.Range("V2")
        .FormulaArray = theFormulaPart1
        .Replace "X_X_X())", theFormulaPart2
        .Replace "X_X_X1()", theFormulaPart3
    End With

End Sub