Office 2003 SP3, VBA: Retail 6.5.1024
I'm sure that I'm missing something obvious but I'm stumped. I'm trying to calculate the 95th percentile of an array of numbers. I've searched the web and found
but all this does is return 17.8400810185185 when I have 17.8400810185185 and 24.3514814814815 as the two values in my test array. The excel percentile function returns something more realistic like 24.02591146 when I create a cell with a formula.Code:Function u_percentile(arr() As Single, k As Single) Dim i As Integer, n As Integer, x As Integer Dim upper As Double On Error Resume Next upper = UBound(arr) If Err.Number Then If Err.Number = 9 Then u_percentile = 0 Exit Function End If End If n = UBound(arr) - 1 Call Sort(arr) x = Application.Max(Application.Min(Int(k * n), n), 1) u_percentile = arr(x) Exit Function End Function
So I figured I'd try using the excel function by calling it from VBA but I seem to be getting the .9 percentile rather than the .95 percentile
but this returns 23.7... which I get when plugging in 0.90 in to the formula cell I created in the worksheet rather than 24.02591146 when I plug 0.95 in to the formula cell I created in the worksheet.Code:Function u_percentile(arr() As Single, k As Single) Dim i As Integer, n As Integer, x As Integer Dim upper As Double On Error Resume Next upper = UBound(arr) If Err.Number Then If Err.Number = 9 Then u_percentile = 0 Exit Function End If End If u_percentile = Application.WorksheetFunction.Percentile(arr(), 0.95) exit function End Function
Is there some way to force the call to Percentile to use 0.95 rather than 0.9?
Thank you in advance





Reply With Quote