Excel VBA - 如何添加动态数组公式

发布于 2025-01-16 07:15:40 字数 558 浏览 2 评论 0原文

我通过 VBA 在工作表中添加一个公式,应该是:

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

这利用 Excel 中新的 SPILL 功能为我提供 B 列值的列表,其中 A 列中的相关值与单元格 A 中的值相匹配。 UNIQUE 函数可删除任何多个空白 ("") 结果。

如果我手动在 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-01-23 07:15:40

好问题,我查了一下...


简而言之:

使用=Cells(x,y).Formula2 而不是 =Cells(x,y).Formula


说明:

@ 显示的称为隐式交集运算符。来自微软文档:

隐式交集逻辑将许多值减少为单个值。 Excel 这样做是为了强制公式返回单个值,因为
单元格只能包含单个值。如果您的公式返回
一个值,然后隐式交集什么也不做(即使它
从技术上讲是在后台完成的)。

但是为什么它会出现在较新的 Excel O365 中?好吧,Range.Formula 使用 IIE(隐式交集),因此添加 @ 基本上可以撤消动态数组功能。 UNIQUE 是一个新的动态数组函数。因此,要在代码中写出此内容,您应该使用 Range.Formula2 属性(或 Range.Formula2R1C1(如果您使用 R1C1 表示法)。这些属性使用 AE(数组求值),现在是默认值。

  • 这里是 MS 就该主题提供的信息丰富的文档,其中更详细地解释了 FormulaFormula2 之间的区别。

  • 如果您想了解有关隐式交集运算符的更多信息,请查看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 和您的相关数据。
原文