如何将Excel中多单元格数组公式的结果放入VBA中的数组?

huangapple go评论64阅读模式
英文:

How can I put the result of a multi cell array formula in Excel into an array in VBA?

问题

我有一个相对复杂(我认为是)的公式,它会产生一个多单元格数组公式。用户在Excel的界面工作表上单击按钮,我希望这可以触发一些VBA代码,将多单元格数组公式的数组结果放入VBA子程序中。

我需要使用的公式如下:
=FILTER(OFFSET(INDIRECT(Results!O22),0,-3),MOD(SEQUENCE(1,COLUMNS(INDIRECT(Results!O22)),5)=0)

Results!O22中的值是:
= "SumSheet_OffTrt" & "!" & ADDRESS(ROW(rng_Data_start_ALL),MATCH(O23,INDIRECT("SumSheet_OffTrt" & "!" & ROW(rng_AddRows_ALL)& ":" & ROW(rng_AddRows_ALL)),0)) & ":" & ADDRESS(ROW(rng_Data_start_ALL) + COUNTA(INDIRECT("SumSheet_OffTrt" & "!" & CHAR(64 + COLUMN(OFFSET(rng_Data_start_ALL,1,0)))& ":" & CHAR(64 + COLUMN(OFFSET(rng_Data_start_ALL,1,0))))) - 1,MATCH(P$6,INDIRECT("SumSheet_OffTrt" & "!" & ROW(rng_AddRows_ALL)& ":" & ROW(rng_AddRows_ALL)),0)-1)

这个疯狂的公式在Results!O22中的结果是:
SumSheet_OffTrt!$BY$14:$CR$68,这是一些数据存储的范围。

当手动在Excel中输入上述提供的FILTER公式时,该公式会在工作表中生成一个可用的多单元格数组。

然而,当我尝试使用VBA将公式输入Excel时,它不起作用。

注意,我使用的是Excel的瑞典语版本,其中分隔符是";"。我还尝试在VBA代码中使用","来进行所有讨论的操作。

我尝试了以下方法:

  1. 使用Application.Evaluate方法将数组的值设置为公式的结果:
Dim strFrmla As String
Dim aryRsltData As Variant
strFrmla = "FILTER(OFFSET(INDIRECT(Results!O22),0,-3),MOD(SEQUENCE(1,COLUMNS(INDIRECT(Results!O22)),5)=0)"
aryRsltData = Application.Evaluate(strFrmla)

在这种情况下,aryRsltData的值是错误2015,或VALUE!。

  1. 将Range的值、公式或FormulaArray设置为公式字符串变量,以将数组设置为结果范围的值:
Dim strFrmla As String
strFrmla = "FILTER(OFFSET(INDIRECT(Results!O22),0,-3),MOD(SEQUENCE(1,COLUMNS(INDIRECT(Results!O22)),5)=0)"
Sheet2.Range("A1").FormulaArray = strFrmla

上述代码的最后一行触发以下错误:"Application-defined or object-defined error"。如果我使用.Value.Formula也会出现相同的错误。

  1. 我尝试阻止Excel对值输入的响应,然后使用VBA放置公式,然后恢复Application的默认设置:
Dim strFrmla As String

Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.DisplayStatusBar = False

strFrmla = "FILTER(OFFSET(INDIRECT(Results!O22),0,-3),MOD(SEQUENCE(1,COLUMNS(INDIRECT(Results!O22)),5)=0)"
Sheet2.Range("A1").FormulaArray = strFrmla

Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.Calculate

生成了与尝试#2相同的错误。

  1. 我尝试将公式作为文本粘贴到范围中,然后清除格式,然后计算。
Dim strTargFrmla As String
strFrmla = "FILTER(OFFSET(INDIRECT(Results!O22),0,-3),MOD(SEQUENCE(1,COLUMNS(INDIRECT(Results!O22)),5)=0)"
Sheet2.Range("A1").FormulaArray = "'" & strFrmla
Sheet2.Range("A1").ClearFormats
Application.Calculate

这将公式放入单元格中,即使未格式化,但用户仍然需要单击单元格并按Enter键,以填充多单元格数组。

有一个类似的讨论,我尝试了其中与我的操作接近的建议:
https://stackoverflow.com/questions/50030749/insert-array-formula-in-excel-vba
我已经查看了其他几个帖子,似乎找不到解决方案。

最终,我尝试的目标只是将多单元格数组的结果放入VBA中的一个数组中,以便我可以继续添加工作簿的特定功能。

非常感谢任何人可以提供的支持。我相信还有其他方法可以达到我的目标,所以如果这里没有出现任何问题,我将尝试从头开始。

非常感谢!

英文:

I have a relatively complex (I think) formula that results in a multi cell array formula. The user clicks a button on the interface sheet in Excel, and I want this to trigger some VBA code that puts the array result of the multi cell array formula into an array within a VBA sub.

The formula I need to use is the following:
=FILTER(OFFSET(INDIRECT(Results!O22);0;-3);MOD(SEQUENCE(1;COLUMNS(INDIRECT(Results!O22)));5)=0)

The value in Results!O22 is:
="SumSheet_OffTrt"&"!"&ADDRESS(ROW(rng_Data_start_ALL);MATCH(O23;INDIRECT("SumSheet_OffTrt"&"!"&ROW(rng_AddRows_ALL)&":"&ROW(rng_AddRows_ALL));0))&":"&ADDRESS(ROW(rng_Data_start_ALL) + COUNTA(INDIRECT("SumSheet_OffTrt"&"!"&CHAR(64 + COLUMN(OFFSET(rng_Data_start_ALL;1;0)))&":"&CHAR(64 + COLUMN(OFFSET(rng_Data_start_ALL;1;0)))))-1;MATCH(P$6;INDIRECT("SumSheet_OffTrt"&"!"&ROW(rng_AddRows_ALL)&":"&ROW(rng_AddRows_ALL));0)-1)

The result of the this crazy formula in Results!O22 is:
SumSheet_OffTrt!$BY$14:$CR$68, the range where some data are stored.

When the FILTER formula provided above is manually entered into Excel, the formula produces a functional multi cell array in the worksheet.

However, when I try to input the formula into Excel using VBA, it does not work.

Note, I am using a Swedish version of Excel, where the ";" is the separator. I have also attempted everything discussed below using a "," within the VBA code.

I have tried the following:

  1. Using the Application.Evaluate method to set the value of an array equal to the formula result:
Dim strFrmla As String
Dim aryRsltData As Variant
strFrmla = =FILTER(OFFSET(INDIRECT(Results!O22);0;-3);MOD(SEQUENCE(1;COLUMNS(INDIRECT(Results!O22)));5)=0)
aryRsltData  = Application.Evaluate(strFrmla)

In this case, the aryRsltData has a value of Error 2015, or VALUE!.

  1. Setting the value, formula, or formulaarray of a Range equal to a string variable of the formula, with the intention of setting the array equal to the resulting range value:
Dim strFrmla As String
strFrmla = =FILTER(OFFSET(INDIRECT(Results!O22);0;-3);MOD(SEQUENCE(1;COLUMNS(INDIRECT(Results!O22)));5)=0)

Sheet2.Range("A1").FormulaArray = strFrmla

The last line of code above triggers the following error: "Application-defined or object-defined error". Same error if I use .Value or .Formula.

  1. I tried preventing Excel from responding to the value input, then I use VBA to place the formula, and then return Application defaults:
Dim strFrmla As String

Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.DisplayStatusBar = False

strFrmla = =FILTER(OFFSET(INDIRECT(Results!O22);0;-3);MOD(SEQUENCE(1;COLUMNS(INDIRECT(Results!O22)));5)=0)

Sheet2.Range("A1").FormulaArray = strFrmla

Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.Calculate

The generated the same error as attempt #2.

  1. I tried pasting the formula as text to the range, then clearing formats, and then calculating.
Dim strTargFrmla As String
strFrmla = =FILTER(OFFSET(INDIRECT(Results!O22);0;-3);MOD(SEQUENCE(1;COLUMNS(INDIRECT(Results!O22)));5)=0)

Sheet2.Range("A1").FormulaArray = "'" & strFrmla
Sheet2.Range("A1").ClearFormats
Application.Calculate

This gets the formula into the cell, even unformatted, but the user still needs to click the cell and press enter for the multi cell array to populate.

There was one similar discussion, and I have tried the suggestions where they seemed close to what I was doing:
https://stackoverflow.com/questions/50030749/insert-array-formula-in-excel-vba
I have reviewed several other threads and cannot seem to find a solution.

Ultimately, what I am trying to do is simply get the resulting multi cell array into an array within VBA, so that I can proceed with adding a specific feature to the workbook.

Many thanks for any support anyone can provide. I am sure there are other ways I can reach my goal, so if nothing comes up here, I'll try again from the ground up.

Many thanks!

答案1

得分: 1

在原帖得到评论者的支持下,找到了一个解决方案。通过将strFrmla设置为=FILTER(OFFSET(INDIRECT('Results'!R22C15),0,-3),MOD(SEQUENCE(1,COLUMNS(INDIRECT('Results'!R22C15))),5)=0),然后运行以下代码解决了问题:
Sheet2.Range("A1").Formula2R1C1 = strFrmla

出于某种原因,使用R1C1引用样式起效果。这是通过使用录制宏功能来识别的,并手动粘贴功能性的多单元格数组公式,正如评论中建议的那样。

感谢大家的支持,如果你理解为什么会这样,请随时发表评论。

英文:

With the support of those commenting on the original post, a solution was found. Using the R1C1 reference style by setting strFrmla equal to =FILTER(OFFSET(INDIRECT('Results'!R22C15),0,-3),MOD(SEQUENCE(1,COLUMNS(INDIRECT('Results'!R22C15))),5)=0), and then running the following solved the issue:
Sheet2.Range("A1").Formula2R1C1 = strFrmla

For some reason, using the R1C1 reference style worked. This was identified by using the record macro feature, and pasting the functional multi cell array formula manually as suggested in the comments.

Thanks all for the support, and feel free to comment if you understand why this is the case.

huangapple
  • 本文由 发表于 2023年6月1日 17:48:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定