【问题标题】:Range works in formula but not in custom function范围适用于公式,但不适用于自定义函数
【发布时间】:2021-12-19 18:14:23
【问题描述】:

我在公式中有一个范围并且它有效:

=MAX(LN(A2:A7))

现在我想将此公式重新构建为自定义函数,但它不起作用。

Function testy(xr As Range)
    n = WorksheetFunction.Max(WorksheetFunction.Ln(xr))
    testy = n   
End Function

它抛出一个错误:错误的数据类型。但是我定义了“作为范围”? 为什么它不起作用,我该如何纠正它?

【问题讨论】:

    标签: vba excel-formula range


    【解决方案1】:

    使用Application.Ln

    问题在于WorksheetFunction.Ln 接受Double 作为其参数。你隐式地传递了一个多单元格范围的.Value,这是一个Variant 数组。

    使用后期绑定Application.Ln,可以传递一个数组(并返回一个数组):

    Function testy(ByVal xr As Range) As Double
        testy = WorksheetFunction.Max(Application.Ln(xr))
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多