如何将 A1:A500 等数组数据传递给 Excel 用户定义函数

How to pass array data such as A1:A500 into a Excel user defined function

我正在尝试将变量数组传递给 excel 用户定义的函数。我的 Excel VBA 附在下面,我希望我可以将诸如 A1: A100 的范围传递到当前变量数据中,然后像正常操作一样操作它 excel formula.Any 将不胜感激。

 Function LowerB_4n_2(inputrange As Range) As Double
 Dim myArray As Variant
 Data = inputrange.Value
 LowerB_4n_2 = Small(Data, (Round((((0.4 * (Count(Data))) - 2)), 0)))
 End Function

 Function UpperB_4n_2(inputrange As Range) As Double
 Dim myArray As Variant
 Data = inputrange.Value
 UpperB_4n_2 = Large(Data, (Round((((0.4 * Count(Data)) - 2)), 0)))
 End Function

 Function Width4n_2(inputrange As Range) As Double
 Dim myArray As Variant
 Data = inputrange.Value
  Width4n_2 = (Large(Data, (Round((((0.4 * Count(Data)) - 2)), 0))) - 
  (Small(Data, (Round((((0.4 * Count(Data)) - 2)), 0)))))

  End Function

Large、Small 和 Count 都是 Application.WorksheetFunction 的属性,因此它们需要将其设置为父项。

这是你的第一个:

 Function LowerB_4n_2(inputrange As Range) As Double
 Dim data() As Variant
 data = inputrange.Value
 LowerB_4n_2 = Application.WorksheetFunction.Small(data, (Round((((0.4 * (Application.WorksheetFunction.Count(data))) - 2)), 0)))
 End Function

根据评论,

由于Large和Small已经精简,没有理由将范围值传递到数组中。我们可以只引用公式中的范围对象:

 Function LowerB_4n_2(inputrange As Range) As Double
 LowerB_4n_2 = Application.WorksheetFunction.Small(inputrange, (Round((((0.4 * (Application.WorksheetFunction.Count(inputrange))) - 2)), 0)))
 End Function

 Function UpperB_4n_2(inputrange As Range) As Double
 UpperB_4n_2 = Application.WorksheetFunction.Large(inputrange, (Round((((0.4 * (Application.WorksheetFunction.Count(inputrange))) - 2)), 0)))
 End Function

 Function Width4n_2(inputrange As Range) As Double
 With Application.WorksheetFunction
     Width4n_2 = .Large(inputrange, (Round((((0.4 * .Count(inputrange)) - 2)), 0)) - .Small(inputrange, (Round((((0.4 * .Count(inputrange)) - 2)), 0))))
 End With