如何在UDF中引用一系列单元格 [英] How to reference a range of cells in UDF
问题描述
c#,ExcelDNA,在我的UDF中,我想传递一系列单元格MyUDF(A1:A2)
c#, ExcelDNA, in my UDF, I want to pass a range of cells, MyUDF(A1:A2)
我尝试了MyUDF(object param1),如果我键入myUDF(A1:A2),则永远不会调用MyUDF我也尝试了MyUDF(string param1),MyUDF(string [] param1)都行不通.
I tried MyUDF(object param1), if I type in myUDF(A1:A2), the MyUDF is never called I also tried MyUDF(string param1), MyUDF(string[] param1) neither works.
该参数应为哪种类型?谢谢
What type of the parameter should be? thanks
推荐答案
格式公共静态对象MyUDF(object param1){...}
应该可以正常工作,并且您的公式与= MyUDF(A1:A2)
将传入具有这些单元格中的值的对象数组.
The format public static object MyUDF(object param1) {...}
should work fine, and your formula with =MyUDF(A1:A2)
will pass in an object array with the values in those cells.
如果要允许该参数作为对传入范围的引用,则将其声明为
If you want to allow the argument to be a reference to the passed in range, you'd declare it as
public static object MyUDF(
[ExcelArgument(AllowReference=true)] object param1) {...}
额外属性告诉Excel在调用 = MyUDF(A1:A2)
时传递实际引用-引用的类型为 ExcelDna.Integration.ExcelReference
,您可以从中获取值或进一步调用Excel API以获取地址或其他任何内容.
The extra attribute tells Excel to pass in the actual reference when called as =MyUDF(A1:A2)
- the reference will be of type ExcelDna.Integration.ExcelReference
, from where you can get the values or make further calls to the Excel API to get the address or anything.
这篇关于如何在UDF中引用一系列单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!