即使将公式拆分成大约250个字符的限制,也无法用VBA插入长数组公式

时间:2019-03-23 14:34:57

标签: excel vba excel-formula

我正在尝试使用VBA将数组公式填充到单元格中。我的公式超过250个字符,因此我将其分为六个部分。但是,只要我运行代码,我仍然会收到“无法设置Range类的FormulaArray属性”错误。

相关行:

Dim Formulapart1 As String
Dim Formulapart2 As String
Dim Formulapart3 As String
Dim Formulapart4 As String
Dim Formulapart5 As String
Dim Formulapart6 As String

Formulapart1 = "=INDEX(11111;MATCH(1;($A$1=22222)*(G$1=33333)*($S$5=44444);0);MATCH($S$2;55555;0))"
Formulapart2 = "'\\common-fs-01\supervisors\resource optimization\resourcing tables 2019\[queries_all_v2.xlsx]sheet1'!$a$1:$u$22788"
Formulapart3 = "'\\common-fs-01\supervisors\resource optimization\resourcing tables 2019\[queries_all_v2.xlsx]sheet1'!$Q$1:$Q$22788"
Formulapart4 = "'\\common-fs-01\supervisors\resource optimization\resourcing tables 2019\[queries_all_v2.xlsx]sheet1'!$H$1:$H$22788"
Formulapart5 = "'\\common-fs-01\supervisors\resource optimization\resourcing tables 2019\[queries_all_v2.xlsx]sheet1'!$M$1:$M$22788"
Formulapart6 = "'\\common-fs-01\supervisors\resource optimization\resourcing tables 2019\[queries_all_v2.xlsx]sheet1'!$A$1:$U$1"

With wb.Worksheets(1).Range("G4")
    .FormulaArray = Formulapart1
    .Replace "11111", Formulapart2
    .Replace "22222", Formulapart3
    .Replace "33333", Formulapart4
    .Replace "44444", Formulapart5
    .Replace "55555", Formulapart6
End With

重申一下,我只想将公式粘贴到单元格G4中,但无法弄清楚为什么它不起作用。我已经阅读了有关该主题的多个讨论和文章,无法弄清楚我在做什么错。

1 个答案:

答案 0 :(得分:0)

如@GSerg所述,FormulaArray仅接受英语样式公式。 因此,更改此行即可行(我在Excel上进行了测试):

Formulapart1 = "=INDEX(11111,MATCH(1,($A$1=22222)*(G$1=33333)*($S$5=44444),0),MATCH($S$2,55555,0))"

为方便起见,这是完整的修改后的代码:

Dim Formulapart1 As String
Dim Formulapart2 As String
Dim Formulapart3 As String
Dim Formulapart4 As String
Dim Formulapart5 As String
Dim Formulapart6 As String

Formulapart1 = "=INDEX(11111,MATCH(1,($A$1=22222)*(G$1=33333)*($S$5=44444),0),MATCH($S$2,55555,0))"
Formulapart2 = "'\\common-fs-01\supervisors\resource optimization\resourcing tables 2019\[queries_all_v2.xlsx]sheet1'!$a$1:$u$22788"
Formulapart3 = "'\\common-fs-01\supervisors\resource optimization\resourcing tables 2019\[queries_all_v2.xlsx]sheet1'!$Q$1:$Q$22788"
Formulapart4 = "'\\common-fs-01\supervisors\resource optimization\resourcing tables 2019\[queries_all_v2.xlsx]sheet1'!$H$1:$H$22788"
Formulapart5 = "'\\common-fs-01\supervisors\resource optimization\resourcing tables 2019\[queries_all_v2.xlsx]sheet1'!$M$1:$M$22788"
Formulapart6 = "'\\common-fs-01\supervisors\resource optimization\resourcing tables 2019\[queries_all_v2.xlsx]sheet1'!$A$1:$U$1"

With wb.Worksheets(1).Range("G4")
    .FormulaArray = Formulapart1
    .Replace "11111", Formulapart2
    .Replace "22222", Formulapart3
    .Replace "33333", Formulapart4
    .Replace "44444", Formulapart5
    .Replace "55555", Formulapart6
End With

希望这会有所帮助。