使用响应 'answer' 查找它在列中的百分位数

Using a response 'answer' to find what percentile it is within a column

该代码目前要求用户提供两个单独的号码,并将它们分开,然后在消息框中弹出答案。接下来我要做的是使用该答案并计算出它在我的工作簿的 "T" 列中的百分位数。

 If response = vbNo Then
    Dim cost, weight, answer As Variant
        cost = InputBox("Please Enter PO Cost")
        weight = InputBox("Please Enter Net Weight")
        answer = cost / weight
        MsgBox "Price per KG is: " & answer
        Exit Sub 

您可以按如下方式使用百分位数工作表函数。

Public Sub Percentile()
    Dim myrng As Range

    Set myrng = Range("t1:t10") 'set the range
    mypercentile = WorksheetFunction.Percentile(myrng, 0.9) ' Retrieve the 90th percentile from t1:t10
    MsgBox ("The percentile is " & mypercentile)
End Sub

您可以使用 Excel 的内置 Percentrank 函数。我假设 T 列中没有任何空白。

Dim x as double
Set ws= ActiveWorkbook.Worksheets("Sheet1")
Set relevant_array = ws.Range(ws.Range("T1"),ws.Range("T1").End(xlDown))
x = WorksheetFunction.Percentrank(relevant_array.Address,answer)
debug.print x