使用 VBA 将 ArrayFormula 设置为多个 Excel 单元格
我有一个输出单个值的数组公式,我想为一大堆单元格提供相同的数组公式。问题是,当我将数组公式分配给范围时,它会以这样的方式解释公式:它们都共享对数组公式的单个调用的输出,而不是每个公式都输出单独的值。
为了向您展示我的意思,我使用以下代码:
With MarginalData
.Range(.Cells(2, 1), .Cells(13, .UsedRange.Columns.Count)).FormulaArray = pullFormula
End With
我想要的,是一个如下所示的结果:
这就是我在范围内的每个单元格中单独输入数组公式时的样子。
但是我得到的是这样的:
第一个单元格中数组公式的输出在所有列中重复 - 它们都共享相同的输出。
如何以编程方式分配数组公式,就像每个单元格都单独分配一样?
公式为:
{=INDEX(BatchResults,MATCH(TTID&CHAR(1)&ROW()-1,BatchResultsTTIDS&CHAR(1)&BatchResultsLayers,0),MATCH(A$1,BatchTTIDData!$1:$1,0 ))}
它必须作为数组公式放入,因为它不是在单个列上执行匹配,而是在两个串联列上执行匹配。列的串联必须作为数组返回,因此公式必须作为数组公式输入。
迄今为止最简单的解决方案(下面接受的答案的变体)如下:
Const pullFormula = "=INDEX(BatchResults,MATCH(TTID&CHAR(1)&ROW()-1,BatchResultsTTIDS&CHAR(1)&BatchResultsLayers,0),MATCH(A$1,BatchTTIDData!$1:$1,0))"
With wrksht
With .Range(.Cells(2, 1), .Cells(13, .UsedRange.Columns.Count))
.Formula = pullFormula
.FormulaArray = .FormulaR1C1
End With
End With
I have an array formula that outputs a single value, and I want to give a whole bunch of cells this same array formula. The problem is when I assign the array formula to the range, it interprets the formula in such a way as them all sharing the output of a single call to the array formula, rather than each of them outputting a separate value.
To show you what I mean, I'm using the following code:
With MarginalData
.Range(.Cells(2, 1), .Cells(13, .UsedRange.Columns.Count)).FormulaArray = pullFormula
End With
What I want, is a result that looks like this:
That is what it looks like when I enter the array formula separately in every cell in the range.
But what I get is this:
The output of the array formula in the first cell is repeated in all the columns - they all share the same output.
How can I programatically assign the array formula as though each cell had it assigned separately?
The formula is:
{=INDEX(BatchResults,MATCH(TTID&CHAR(1)&ROW()-1,BatchResultsTTIDS&CHAR(1)&BatchResultsLayers,0),MATCH(A$1,BatchTTIDData!$1:$1,0))}
It must be put in as an array formula because it performs a match not on a single column, but on two concatenated columns. The concatenation of the columns must be returned as an array, hence the formula must be entered as an array formula.
The simplest solution so far, a variant of the accepted answer below, is the following:
Const pullFormula = "=INDEX(BatchResults,MATCH(TTID&CHAR(1)&ROW()-1,BatchResultsTTIDS&CHAR(1)&BatchResultsLayers,0),MATCH(A$1,BatchTTIDData!$1:$1,0))"
With wrksht
With .Range(.Cells(2, 1), .Cells(13, .UsedRange.Columns.Count))
.Formula = pullFormula
.FormulaArray = .FormulaR1C1
End With
End With
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或者选择数组公式为 R1C1,将范围指定为 FormulaR1C1,然后将 FormulaR1C1 指定为数组公式。假设数组公式位于单元格 A2 中
Or pick up the Array Formula as R1C1, assign to the range as FormulaR1C1, then assign the FormulaR1C1 as Array Formula. This assumes Array Formula is in cell A2
尝试半自动进行。为第一行设置公式,然后使用 FillDown。
Try to do it semi-automatically. Set formula for the first row, then use FillDown.
尝试代替 1 澳元
Instead of A$1, try