Results 1 to 11 of 11

User-defined function to calculate quartiles

Threaded View

  1. #1
    Forum Contributor
    Join Date
    08-29-2011
    Location
    Atlanta
    MS-Off Ver
    Excel 2007
    Posts
    171

    User-defined function to calculate quartiles

    Hi everyone, I am working on a UDF to calculate quartiles (the ones provided in Excel do not calculate the way I want). However, when I call on the function and type in the parameters, I get a VALUE error. Can someone help me with where I'm going wrong?

    (btw, this is my first UDF, so I apologize if something is fundamentally wrong with it)

    Thanks!


    Function QuartileNorm(MyRange As Range, Quartile As Integer)
    
    Dim Q0 As Long
    Dim Q1 As Long
    Dim Q2 As Long
    Dim Q3 As Long
    Dim Q4 As Long
    Dim rng As Long
    
    
    Q0 = WorksheetFunction.Min(MyRange)
    Q4 = WorksheetFunction.Max(MyRange)
    
    rng = WorksheetFunction.Count(MyRange)
    If rng Mod 4 = 0 Then
        Q1 = (WorksheetFunction.Small(MyRange, rng / 4) + WorksheetFunction.Small(MyRange, (rng / 4) + 1)) / 2
        Q2 = (WorksheetFunction.Small(MyRange, rng / 2) + WorksheetFunction.Small(MyRange, (rng / 2) + 1)) / 2
        Q3 = (WorksheetFunction.Small(MyRange, (rng / 4) + (rng / 2)) + WorksheetFunction.Small(MyRange, (rng / 4) + (rng / 2) + 1)) / 2
      ElseIf rng Mod 4 = 2 Then
        Q1 = WorksheetFunction.Small(MyRange, (rng / 4) + 0.5)
        Q2 = (WorksheetFunction.Small(MyRange, rng / 2) + WorksheetFunction.Small(MyRange, (rng / 2) + 1)) / 2
        Q3 = WorksheetFunction.Small(MyRange, (rng / 4) + (rng / 2) + 0.5)
      ElseIf rng Mod 4 = 1 Then
        Q1 = WorksheetFunction.Small(MyRange, (((rng / 2) + 0.5) / 2) + 0.5)
        Q2 = WorksheetFunction.Small(MyRange, rng / 2 + 0.5)
        Q3 = WorksheetFunction.Large(MyRange, (((rng / 2) + 0.5) / 2) + 0.5)
      ElseIf rng Mod 4 = 3 Then
        Q1 = (WorksheetFunction.Small(MyRange, (((rng / 2) + 0.5) / 2)) + WorksheetFunction.Small(MyRange, (((rng / 2) + 0.5) / 2) + 1)) / 2
        Q2 = WorksheetFunction.Small(MyRange, rng / 2 + 0.5)
        Q3 = (WorksheetFunction.Large(MyRange, (((rng / 2) + 0.5) / 2)) + WorksheetFunction.Small(MyRange, (((rng / 2) + 0.5) / 2) + 1)) / 2
    End If
    
    If Quartile = 0 Then
        QuartileNorm = Q0
      ElseIf Quartile = 1 Then
        QuartileNorm = Q1
      ElseIf Quartile = 2 Then
        QuartileNorm = Q2
      ElseIf Quartile = 3 Then
        QuartileNorm = Q3
      ElseIf Quartile = 4 Then
        QuartileNorm = Q4
    End If
    
    
    End Function
    Last edited by ATLGator; 06-18-2014 at 09:50 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] User defined function returns an error on a standard function used in it.
    By pb48 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-23-2013, 01:35 PM
  2. Excel 2007 : User defined search and calculate
    By PunterHunter in forum Excel General
    Replies: 4
    Last Post: 10-20-2011, 09:20 AM
  3. User defined function to calculate difference
    By antonymiller in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-16-2010, 03:41 PM
  4. Excel - User Defined Function Error: This function takes no argume
    By BruceInCalgary in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-18-2006, 04:05 PM
  5. [SOLVED] calculate now for user defined functions?
    By Mike in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-28-2006, 03:10 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1