Excel VBA - 如何添加动态数组公式
我通过 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好问题,我查了一下...
简而言之:
使用
=Cells(x,y).Formula2 而不是
=Cells(x,y).Formula
说明:
@ 显示的称为隐式交集运算符。来自微软文档:
但是为什么它会出现在较新的 Excel O365 中?好吧,
Range.Formula
使用 IIE(隐式交集),因此添加@
基本上可以撤消动态数组功能。UNIQUE
是一个新的动态数组函数。因此,要在代码中写出此内容,您应该使用Range.Formula2
属性(或Range.Formula2R1C1
(如果您使用R1C1
表示法)。这些属性使用 AE(数组求值),现在是默认值。这里是 MS 就该主题提供的信息丰富的文档,其中更详细地解释了
Formula
和Formula2
之间的区别。如果您想了解有关隐式交集运算符的更多信息,请查看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: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 theRange.Formula2
property (orRange.Formula2R1C1
if you useR1C1
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
andFormula2
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.