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

我正在尝试将一个variables数组传递给一个Excel用户定义的函数。 我的Excel VBA附在下面,我希望我可以通过一个范围,如A1:A100到当前的variables数据,然后像一个正常的Excel公式一样操纵它。任何帮助将不胜感激。

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 

根据评论,

由于大和小已经精简,没有理由将范围值传递给数组。 我们可以参考公式中的范围对象:

  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