1

percentrank 函数可以为其两个参数采用相同的命名范围,例如:

=percentrank(inputrange,inputrange)

它将第一个参数视为用于计算截断百分比的单元格范围,并使用第二个参数返回单个单元格的值以在截断值中排名。单个单元格由输入/调用函数的行确定。

我想重新创建此功能并使用第二个范围引用来查找单个单元格值(基于输入函数的行)。这是我所拥有的:

Public Function QUARTILE_RANK(DataRange As Range, RefCell As Range)

If RefCell <> vbNullString Then

    q1 = Application.WorksheetFunction.Quartile(DataRange, 1)
    q2 = Application.WorksheetFunction.Quartile(DataRange, 2)
    q3 = Application.WorksheetFunction.Quartile(DataRange, 3)
    q4 = Application.WorksheetFunction.Quartile(DataRange, 4)

    If (RefCell <= q1) Then QUARTILE_RANK = 1
    If (RefCell > q1) And (RefCell <= q2) Then QUARTILE_RANK = 2
    If (RefCell > q2) And (RefCell <= q3) Then QUARTILE_RANK = 3
    If (RefCell > q3) Then QUARTILE_RANK = 4
Else
    QUARTILE_RANK = vbNullString
End If

End Function

如果我为两个参数传递相同的命名范围,它会将两个范围视为单元格数组。我希望它以这种方式处理第一个参数,但使用第二个参数找到一个单元格值。也就是说,我希望它反映 PERCENTRANK 函数的功能。

编辑:

Sean Chesire 的意见帮助我让它发挥作用。我欢迎任何改进建议,因为我确信其他人已经编写了相同的功能。

这是最终形式:

Public Function QUARTILE_RANK(DataRange As Range, RefRange As Range)

Dim refCell As Range

If RefRange.Rows.Count > 1 Then
    Set refCell = RefRange.Cells(Application.Caller.Row, 1)
Else
    Set refCell = RefRange ' maybe they only passed a cell reference
End If

If refCell <> vbNullString Then

    q1 = Application.WorksheetFunction.Quartile(DataRange, 1)
    q2 = Application.WorksheetFunction.Quartile(DataRange, 2)
    q3 = Application.WorksheetFunction.Quartile(DataRange, 3)
    q4 = Application.WorksheetFunction.Quartile(DataRange, 4)

    If (refCell <= q1) Then QUARTILE_RANK = 1
    If (refCell > q1) And (refCell <= q2) Then QUARTILE_RANK = 2
    If (refCell > q2) And (refCell <= q3) Then QUARTILE_RANK = 3
    If (refCell > q3) Then QUARTILE_RANK = 4
Else
    QUARTILE_RANK = vbNullString
End If

End Function
4

2 回答 2

0

=LOOKUP(PERCENTRANK(vals,vals),{0,0.25,0.5,0.75},{1,2,3,4})

于 2012-07-30T20:56:15.597 回答
0

这将告诉您从哪个单元格调用公式:

Function testit() As String
testit = Application.Caller.Parent.Parent.Name & " " & Application.Caller.Parent.Name & " " & Application.Caller.Address
End Function

然后,您应该能够测试给定的范围,并确定应该在另一个数组中引用哪个值

于 2012-07-30T19:55:55.237 回答