+ Reply to Thread
Results 1 to 10 of 10

Strange behavior of the COUNTIF function for ranking data

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

    Unhappy Strange behavior of the COUNTIF function for ranking data

    Hello everyone,

    We sometimes use the COUNTIF function to rank data such as: =COUNTIF($A$2:$A$707,">"&A3)+COUNTIF($A$2:A3,A3)

    This formula worked well for me for a long time. But just today, this formula failed: it made the same ranking of 400 for two cells with different values. Is this a bug of the COUNTIF() function?

    The original Excel file is attached here.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    03-20-2015
    Location
    Primarily UK, sometimes NL
    MS-Off Ver
    Work: Office 365 / Home: Office 2010
    Posts
    2,405

    Re: Strange behavior of the COUNTIF function for ranking data

    I have no idea why it's doing that. If you evaluate each portion of the formula separately in the formula bar, starting with the last 'A400', it comes out with 399 as expected.
    It's fixable by adding "="& in front of the last A400:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Regards,
    Aardigspook

    I recently started a new job so am a bit busy and may not reply quickly. Sorry - it's not personal - I will reply eventually.
    If your problem is solved, please go to 'Thread Tools' above your first post and 'Mark this Thread as Solved'.
    If you use commas as your decimal separator (1,23 instead of 1.23) then please replace commas with semi-colons in your formulae.
    You don't need to give me rep if I helped, but a thank-you is nice.

  3. #3
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Strange behavior of the COUNTIF function for ranking data

    Hi,

    I imagine you have encountered a floating point issue with that particular value. =COUNTIF(A400,">"&$A$400) returns 1 rather than 0, which is what causes the error you are seeing.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  4. #4
    Forum Contributor
    Join Date
    01-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003, 2007, 2010, 2019, Microsoft 365.
    Posts
    440

    Re: Strange behavior of the COUNTIF function for ranking data

    Hi, Aardigspook, may I ask if the formula is equivalent to yours?

    =COUNTIF($A$2:$A$707,">"&A400)+COUNTIF($A$2:A400,"="&A400)
    =COUNTIF($A$2:$A$707,">"&A400)+COUNTIF($A$2:A400,A400)

    The "="& can be omitted in the COUNTIF function.

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

    Re: Strange behavior of the COUNTIF function for ranking data

    Hi, xlnitwit, it is really strange. The number 0.0835250107681711% is not too small.

    What is floating point issue?

  6. #6
    Forum Expert
    Join Date
    03-20-2015
    Location
    Primarily UK, sometimes NL
    MS-Off Ver
    Work: Office 365 / Home: Office 2010
    Posts
    2,405

    Re: Strange behavior of the COUNTIF function for ranking data

    Yes, the "="& can be omitted, but if you put it in, you'll find that the correct result is returned. I can't explain why, as the two formulae should work identically.

    Edit: what's even weirder is that if you change the value in A400 *, then change it back, the correct rank (399) is then returned.
    * e.g. from 0.0835250107681711% to 0.0835250107681712%
    Last edited by Aardigspook; 09-01-2016 at 08:29 AM. Reason: Additional info about strange behaviour

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Strange behavior of the COUNTIF function for ranking data

    In fact you only need f2 and then press Enter and the formula will calculate correctly. How was this data originally entered into the cells?

  8. #8
    Forum Contributor
    Join Date
    01-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003, 2007, 2010, 2019, Microsoft 365.
    Posts
    440

    Re: Strange behavior of the COUNTIF function for ranking data

    that is really difficult to explain why The data were copied from an Excel 2003 sheet. Maybe the version is too old.

  9. #9
    Forum Expert
    Join Date
    03-20-2015
    Location
    Primarily UK, sometimes NL
    MS-Off Ver
    Work: Office 365 / Home: Office 2010
    Posts
    2,405

    Re: Strange behavior of the COUNTIF function for ranking data

    @xlnitwit: Pressing F2 then Enter doesn't change the (incorrect) result when I try it.


    When I evaluate just COUNTIF($A$2:A400,A400) (by selecting it in the formula bar and pressing F9), I get:
    =COUNTIF($A$2:$A$707,">"&A400)+1

    which then evaluates to 400.


    If I first evaluate the last A400, I get:
    =COUNTIF($A$2:$A$707,">"&A400)+COUNTIF($A$2:A400,0.00083525010768171)

    then:
    =COUNTIF($A$2:$A$707,">"&A400)+0

    which evaluates to 399.

    As far as I can tell from some random testing, this only occurs for A400 and only if that's entered directly. If I replace the last A400 with "="&A400 the formula works correctly. Same if it's replaced with A400+0 or A400-0.
    Edit (correction): If I replace the last A400 with OFFSET(C400,0,-2), the result is also incorrect - very strange.

    Seems like it's some sort of bug - maybe report it on the MS forums and see if you get a response there?
    Last edited by Aardigspook; 09-01-2016 at 09:27 AM. Reason: Correction

  10. #10
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Strange behavior of the COUNTIF function for ranking data

    It is the first part of the formula that is incorrect
    =COUNTIF($A$2:$A$707,">"&A400)
    That part returns 399 and not 398 as it should. The last part should, and does for me, return 1.

    F2 and enter resolves correctly in 2010 for me.

+ 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. Strange Findlastrow behavior.
    By DECROMAX in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-16-2013, 08:03 PM
  2. [SOLVED] VBA - Strange behavior
    By MVM in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-12-2006, 02:50 PM
  3. [SOLVED] Strange TAB behavior
    By m davidson in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-28-2006, 01:15 PM
  4. Strange behavior for AutoFilter
    By 1scant in forum Excel General
    Replies: 0
    Last Post: 03-02-2006, 04:49 PM
  5. [SOLVED] Strange behavior.
    By Wiley Coyote in forum Excel General
    Replies: 7
    Last Post: 10-18-2005, 12:05 PM
  6. [SOLVED] Strange behavior of InStrRev() function
    By Windowed in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-11-2005, 09:05 PM
  7. Strange VBE Behavior
    By Jeff Robson in forum Excel General
    Replies: 4
    Last Post: 01-14-2005, 07:06 PM

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