+ Reply to Thread
Results 1 to 17 of 17

Problem with COUNTIF formula

  1. #1
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Problem with COUNTIF formula

    When trying to answer a thread I came across this situation when trying to use COUNTIF to reveal duplicates.

    The OP has an 18 digit serial number stored as text, I thought
    Please Login or Register  to view this content.
    Dragged Down
    would show duplicates if the returned value was greater than 0, the result was not at all what I was expecting. (See the attached workbook)

    Step 2 I tried MATCH as a check and this does return what I was expecting.

    Step 3 was to convert the text string to a number and run the COUNTIF again, still seems wrong

    Step 4 concatenated the string with a trailing "A" and the COUNTIF now returns the expected result

    Why does the original formula apparently not work?....


    Slainte
    Alistair
    Attached Files Attached Files
    Last edited by Marcol; 03-06-2011 at 02:13 PM. Reason: Problem Solved
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  2. #2
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Problem with COUNTIF formula

    Check your $ signs in your formula.

  3. #3
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Problem with COUNTIF formula

    Try this:

    =COUNTIF($A$1:$A1,$A2)

  4. #4
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Problem with COUNTIF formula

    Actually I just looked at your sheet and saw that you have headers. So the first COUNTIF() should be:

    =COUNTIF($A$1:$A2,$A2)

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Problem with COUNTIF formula

    Quote Originally Posted by Marcol
    Why does the original formula apparently not work?
    COUNTIF will treat the numeric string as a number and will work to 15 digit significance - hence your results in C.

    To see this "in action":

    Please Login or Register  to view this content.
    M2 onwards will ape C2 onwards

  6. #6
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Problem with COUNTIF formula

    And

    would show duplicates if the returned value was greater than 0
    it shows dupes if returned value greater than 1 - not 0

  7. #7
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Problem with COUNTIF formula

    Hi Cutter,
    That's not the dilema, the same formula works fine with the concatenated string. Column "J"

    The problem seems to be with long integers (overflow? or truncation?), but why does it not work with the original numeric string formatted as text?

  8. #8
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Problem with COUNTIF formula

    I was coming round to that thinking Don (see last post), but why should the MATCH formula read the string as a string of digits and COUNTIF treat the string as a number despite the formatting?

  9. #9
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Problem with COUNTIF formula

    That's just how COUNTIF works - you can't count numeric strings as strings.

    Please Login or Register  to view this content.
    then

    Please Login or Register  to view this content.
    will both return the same result - 3

    COUNTIF will treat things that look like numbers as numbers and will operate to 15 digit significance as you'd expect.

  10. #10
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Problem with COUNTIF formula

    Does sorting work the same way?

    I tried a sort on the serial 'numbers' and they don't sort properly.

  11. #11
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Problem with COUNTIF formula

    I'm behind the curve here, slow broadband connection yet again.

    Cheers Don that indeed explains the apparent discrepancy.

    One final question
    Why 15 characters?
    It doesn't seem to tally with
    Long (long integer) variables are stored as signed 32-bit (4-byte) numbers ranging in value from -2,147,483,648 to 2,147,483,647

  12. #12
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Problem with COUNTIF formula

    Quote Originally Posted by cutter
    Does sorting work the same way?
    I don't really understand the question - the Serials though strings will sort in numerical order given they are of a consistent length.

    The behaviour outlined by Marcol is very much an idiosyncrasy of COUNTIF/SUMIF - at least it is to my knowledge.

    Quote Originally Posted by Marcol
    Why 15 characters
    Excel works to 15 significant digits (thereafter converted to 0) - see: http://support.microsoft.com/kb/65903

  13. #13
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Problem with COUNTIF formula

    Good point Cutter SORT seems to have the same problem with this type of data.

    That I would never have expected!

  14. #14
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Problem with COUNTIF formula

    Sorry for jumping in on another OP's thread but I feel it's related.

    I preformatted a range of cells as text and then took Marcol's list of serial numbers from col A and did Paste Special - Values into that text formatted range.
    I then did a sort ascend on that range.
    Just a quick look at it showed the first 3 not sorted to last characters.
    Result is:

    899720203566848507
    899720203566848945
    899720203566848499

    The first 15 digits are the same for all 3 but the last 3 vary and are not sorted properly.

  15. #15
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Problem with COUNTIF formula

    Did you choose to sort the numbers as text or number ?

    If you opt to sort treating things "that look like a number as a number" then Excel will work to 15 digits - hence the last 3 digits are ignored.
    Conversely should you sort as Text it should sort in "numerical" order given consistent string length.

  16. #16
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Problem with COUNTIF formula

    I wasn't even aware of that distinction. I just hit the Sort Ascend button in the QAT.

    So I guess, by default it sorts by "looks like a number, sort by a number"

    That explains why, by adding an "A" by formula to the serials, then copy paste values to get rid of the formulas, it does sort properly.

    Another thing to keep in mind for long numbers (or their clones). I keep forgetting about that 15 digit limitation.

    Thanks Don and Marcol.

  17. #17
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Problem with COUNTIF formula

    Again behind the cuve.

    This amended workbook illustrates Dons' last point

    Cheers guys.
    Two problems solved for the price of one!
    .
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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