使用 VBA 将 ArrayFormula 设置为多个 Excel 单元格

发布于 2024-11-08 02:26:48 字数 1291 浏览 5 评论 0原文

我有一个输出单个值的数组公式,我想为一大堆单元格提供相同的数组公式。问题是,当我将数组公式分配给范围时,它会以这样的方式解释公式:它们都共享对数组公式的单个调用的输出,而不是每个公式都输出单独的值。

为了向您展示我的意思,我使用以下代码:

With MarginalData
    .Range(.Cells(2, 1), .Cells(13, .UsedRange.Columns.Count)).FormulaArray = pullFormula
End With

我想要的,是一个如下所示的结果: desired result

这就是我在范围内的每个单元格中单独输入数组公式时的样子。

但是我得到的是这样的: given result

第一个单元格中数组公式的输出在所有列中重复 - 它们都共享相同的输出。

如何以编程方式分配数组公式,就像每个单元格都单独分配一样?


公式为:

{=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:
desired result

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:
given result

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦醒时光 2024-11-15 02:26:48

或者选择数组公式为 R1C1,将范围指定为 FormulaR1C1,然后将 FormulaR1C1 指定为数组公式。假设数组公式位于单元格 A2 中

Sub test()

With Sheet1
    pullFormula = .Range("A2").FormulaR1C1
    Set Rng = .Range(.Cells(2, 1), .Cells(13, .UsedRange.Columns.Count))

    Rng.Formula = pullFormula
    Rng.FormulaArray = Rng.FormulaR1C1

End With
End Sub

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

Sub test()

With Sheet1
    pullFormula = .Range("A2").FormulaR1C1
    Set Rng = .Range(.Cells(2, 1), .Cells(13, .UsedRange.Columns.Count))

    Rng.Formula = pullFormula
    Rng.FormulaArray = Rng.FormulaR1C1

End With
End Sub
层林尽染 2024-11-15 02:26:48

尝试半自动进行。为第一行设置公式,然后使用 FillDown。

Private Sub soCopyFormula()

    Dim MarginalData As Worksheet
    Set MarginalData = ActiveWorkbook.Worksheets("Sheet2")
    Dim oRange As Range
    Dim i As Integer

    With MarginalData
        Set oRange = .Range(.Cells(2, 1), .Cells(13, .UsedRange.Columns.Count))
        ' for each column
        For i = 0 To oRange.Columns.Count - 1
            ' set first row
            oRange(1, i).FormulaArray = pullFormula
            ' copy down
            oRange.Columns(i).FillDown
        Next
    End With

End Sub

Try to do it semi-automatically. Set formula for the first row, then use FillDown.

Private Sub soCopyFormula()

    Dim MarginalData As Worksheet
    Set MarginalData = ActiveWorkbook.Worksheets("Sheet2")
    Dim oRange As Range
    Dim i As Integer

    With MarginalData
        Set oRange = .Range(.Cells(2, 1), .Cells(13, .UsedRange.Columns.Count))
        ' for each column
        For i = 0 To oRange.Columns.Count - 1
            ' set first row
            oRange(1, i).FormulaArray = pullFormula
            ' copy down
            oRange.Columns(i).FillDown
        Next
    End With

End Sub
怪异←思 2024-11-15 02:26:48

尝试代替 1 澳元

INDIRECT(ADDRESS(1,COLUMN()))

Instead of A$1, try

INDIRECT(ADDRESS(1,COLUMN()))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文