使用回答“答案”来查找列中的百分位数

代码目前询问用户两个单独的号码,并将它们分开,答案popup消息框中。 接下来我要做的就是使用这个答案,并且在我的工作簿中列出它在“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 

您可以使用Excel的内置百分比function。 我假设列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 

您可以使用百分比工作表函数如下。

 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