Excel VBA-如何添加动态阵列公式

发布于 2025-01-29 15:45:59 字数 530 浏览 1 评论 0原文

我正在通过VBA将公式添加到工作表中,该公式应该是:

=UNIQUE(IF(TableA[ColumnA]=A1,TableA[ColumnB],""))

它利用Excel中的新溢出功能为我提供了B列值列表,其中a列中的相关值a列在单元格中。我也应用了。删除任何多个空白(“”)结果的唯一功能。

如果我将公式手动键入Excel,则可以很好地工作,但是在使用VBA添加公式时,Excel在公式中添加 @符号,并使其显示#value!。

用于添加公式的VBA线是:

=Cells(x,y).Formula = "=UNIQUE(IF(TableA[ColumnA]=A1,TableA[ColumnB],""""))"

Excel中的结果输出是:

=@UNIQUE(IF(TableA[@[ColumnA]]=A1,TableA[ColumnB],""))

发生了什么,我错过了什么?

提前致谢!

I am adding a formula to a worksheet via VBA which should be:

=UNIQUE(IF(TableA[ColumnA]=A1,TableA[ColumnB],""))

This utilises the new SPILL feature in Excel to give me a list of column B values where the related value in column A matches what is in cell A. I'm also applying the UNIQUE function to remove any multiple blank ("") results.

This works perfectly if I manually type the formula into Excel, however in using VBA to add the formula, Excel is adding @ symbols within the formula, and causing it to show #VALUE!.

The VBA line being used to add the formula is:

=Cells(x,y).Formula = "=UNIQUE(IF(TableA[ColumnA]=A1,TableA[ColumnB],""""))"

The resulting output in Excel is:

=@UNIQUE(IF(TableA[@[ColumnA]]=A1,TableA[ColumnB],""))

What is going on, and what have I missed?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

缘字诀 2025-02-05 15:45:59

好问题,我查一下...


简而

代码>而不是=单元格(x,y).formula


解释:

@称为隐式相交操作员。来自MS文档:

隐式相交逻辑将许多值降低到单个值。 excel这样做是为了迫使公式返回一个值,因为
单元只能包含一个值。如果您的公式返回
一个值,然后隐式交叉没有任何作用(即使它
从技术上讲是在后台完成的)。

,为什么它会出现在您的较新的excel O365中?好吧,range.formula使用IIE(隐式交叉点),从而将@添加到基本上撤消您的动态数组功能。 unique 是一个新的动态数组函数。因此,要在代码中写下此内容,您应该使用 range.formula2 属性(或 range.formula2r1c1 如果使用r1c1 note法)。这些属性使用AE(数组评估),现在是默认值。

  • 这里是来自MS的一位信息文档,该主题解释了公式公式2之间的区别。

  • 如果您想进一步了解隐式交集操作员,请查看 this

  • 我在早些时候与隐式相交的另一个问题回答了另一个问题示例实际工作在这里如果发现它很有趣。

Good question, and I looked it up...


In short:

Use =Cells(x,y).Formula2 instead of =Cells(x,y).Formula


Explaination:

The @ that shows is called the implicit intersection operator. From MS docs:

Implicit intersection logic reduces many values to a single value. Excel did this to force a formula to return a single value, since a
cell could only contain a single value. If your formula was returning
a single value, then implicit intersection did nothing (even though it
was technically being done in the background).

But why does it appear in your newer Excel O365? Well, Range.Formula uses IIE (implicit intersection) thus adding the @ to basically undo your dynamic array functionality. UNIQUE is a new dynamic array function. So, to write this out in code, you should use the Range.Formula2 property (or Range.Formula2R1C1 if you use R1C1 notation). These properties use AE (array evaluation) and is now the default.

  • Here is an informative doc from MS on the subject which explains the difference between Formula and Formula2 in more detail.

  • If you want to know more about the implicit intersection operator then have a look at this

  • I answered another question earlier on that involved implicit intersection with an example on how that actually works here if one finds it interesting.

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