+ Reply to Thread
Results 1 to 5 of 5

Percentiles from two different macro

  1. #1
    Forum Contributor
    Join Date
    01-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003, 2007, 2010, 2019
    Posts
    436

    Percentiles from two different macro

    Hello everyone,

    Do you know why the two macros below produce different results? The 2nd one seems to be in line with normal Excel formula. But the 1st does not.

    The sample file is attached here. Thank you.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,596

    Re: Percentiles from two different macro

    That's because CDBL od DIV/0 in row 8 return you 2007


    Please Login or Register  to view this content.
    Actually, if you remove all text then that value drop to 0

  3. #3
    Forum Contributor
    Join Date
    01-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003, 2007, 2010, 2019
    Posts
    436

    Re: Percentiles from two different macro

    This is really odd. Why an error value is converted to Cdbl?

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,813

    Re: Percentiles from two different macro

    Here's what I see happening, and I will start with a disclaimer that I am not really well versed in some of the programming issues.

    As I usually do with something like this, the first thing I do is start adding Stop statements so I can enter debug mode and try to see what is going on. If you are unfamiliar with VBA's debugging tools, this could be a good time to learn. The final version I ended on looks like this.
    Please Login or Register  to view this content.
    The most interesting part was the part about the type mismatch error. The Iserror(c.value) returns true, the c.value+0 is a type mismatch error, so I started to wonder how I even ended up in the middle of this block If. It should have crashed. That's when I looked farther up and noticed the "on error resume next" statement in effect, telling VBA, "If you encounter any kind of run-time error like a type mismatch, just ignore it and move on to the next statement." So VBA is merrily looping through the list, and it comes to the error 2007 div/0 error. When it tries to add 0 to this error, it can't, but it has been told to ignore errors and keep going, so it moves to the next statement inside of the block if and keeps going. It sees the 2007 in the error number and converts that to double and stores the 2007 in the array.

    I do not know why you included the on error resume next statement. It sometimes seems that we frequently include this in our procedures almost reflexively without actually thinking through the actual error trapping and handling we want to do. I would suggest that you go back through your development process. Reconsider why you included the on error resume next statement - what errors are you expecting and trying to trap and what you want to do with each possible error. Then revise your error handler to something more appropriate to the actual errors you end up seeing.

    PS on edit: the site's firewall is being really stubborn about anything that looks like html today. I will see if I can get it to let me post the code and such. Hopefully there is enough in the plain text that it finally allowed me to post for you to understand what is going on.
    PPS on edit: I think I finally got a final version that I like and the the firewall allowed (seemed to be related to my use of "onerror" misspelling of the VBA statement).
    Last edited by MrShorty; 11-04-2016 at 11:33 AM.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Forum Contributor
    Join Date
    01-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003, 2007, 2010, 2019
    Posts
    436

    Re: Percentiles from two different macro

    Hi MrShorty. Your post is such a useful guidance to help me find the errors. UDFs are particularly tricky to debug for errors.

    I will keep your code and learn from it. A big thank you to you

+ 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. Percentiles
    By graeme27uk in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 08-12-2016, 04:32 AM
  2. Percentiles
    By ma701ss in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-18-2015, 11:25 PM
  3. Replies: 6
    Last Post: 07-17-2014, 03:08 AM
  4. [SOLVED] percentiles?
    By JE McGimpsey in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-06-2005, 04:05 AM
  5. [SOLVED] percentiles?-How does excel calculates the percentiles worksheet function?
    By Agnes Goris in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-14-2005, 11:05 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