Results 1 to 11 of 11

Thread: [RESOLVED] Problems passing decimal value to worksheet application Percentile

Threaded View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    29

    Resolved [RESOLVED] Problems passing decimal value to worksheet application Percentile

    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

    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
    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.

    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

    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
    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.

    Is there some way to force the call to Percentile to use 0.95 rather than 0.9?

    Thank you in advance
    Last edited by PinInCalgary; Feb 8th, 2010 at 05:47 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width