+ Reply to Thread
Results 1 to 11 of 11

User-defined function to calculate quartiles

  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!


    Please Login or Register  to view this content.
    Last edited by ATLGator; 06-18-2014 at 09:50 AM.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,013

    Re: User-defined function to calculate quartiles

    The code itself looks basically ok at first glance, although it needs an error handler (e.g. there are no numbers in the range, or there are error values). Can you post a workbook showing it failing?
    Everyone who confuses correlation and causation ends up dead.

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

    Re: User-defined function to calculate quartiles

    The formula is not producing an error anymore, but its not giving me the results I expected. Here's a sample file.
    Attached Files Attached Files

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,013

    Re: User-defined function to calculate quartiles

    Part of the problem is that your Q0, Q1 etc variables should be Double, not Long.

    Also, in your "rng Mod 4 = 3" section, I think you should have:
    Please Login or Register  to view this content.
    Last edited by romperstomper; 06-18-2014 at 10:18 AM.

  5. #5
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,301

    Re: User-defined function to calculate quartiles

    WorksheetFunction.mod is not available to VBA - use VBA's MOD instead, like so:

    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,013

    Re: User-defined function to calculate quartiles

    BTW, Percentile returns the same results, for this range at least.

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

    Re: User-defined function to calculate quartiles

    for this range, but when you start changing the amount of numbers in the range, the expected values don't come up.

    Try:
    73
    77
    79
    80
    84
    85
    86
    86
    87
    91
    92
    94


    With my formula, Q1 should be 79.5 and Q3 should be 89. Percentile gives you 79.75 and 88, respectively.

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

    Re: User-defined function to calculate quartiles

    romperstomper - I updated all to Double, and I actually caught the SMALL after posting, but did change it

    Bernie - Worksheetfunction.mod was in the original posting, but I quickly updated after see MOD in another post (obviously, not quickly enough). I did make that change though.

    Anything else you see?

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

    Re: User-defined function to calculate quartiles

    Updated code:

    Please Login or Register  to view this content.

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

    Re: User-defined function to calculate quartiles

    Success, thanks for all your help:

    Please Login or Register  to view this content.

  11. #11
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,301

    Re: User-defined function to calculate quartiles

    Try it this way -

    Please Login or Register  to view this content.
    Last edited by Bernie Deitrick; 06-18-2014 at 11:45 AM.

+ Reply to Thread

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