+ Reply to Thread
Results 1 to 30 of 30

Countif function goes crazy

  1. #1
    Registered User
    Join Date
    09-30-2012
    Location
    Tel-Aviv
    MS-Off Ver
    Excel 2010
    Posts
    84

    Countif function goes crazy

    Hi all,
    I have a set of about 30K records (and a strong enough machine to handle processing data). Some of the records are duplicates, so in order to clean up and understand the data better, here is what I did in order to get a frequency table of the duplicates:

    1. Copied the data to a new sheet, performed "remove duplicates".
    2. In column B, next to each value in this short list, I ran a countif function, with =countif(range in the original sheet,value in the short list).
    I remembered to ctrl+shift+enter and dragged the formula to all values.

    However, the sum of column B is GREATER than the number of records in the original file......... I don't understand how that could be!
    Another strange thing is that everytime I run it, one value (couldn't find any pattern as to which value), gets an absurdly high count, such as 3170.
    (However, even with excluding this high number, the total count still doesn't match).

    Anyone had any similar experiences?
    Can you suggest what happened?

    Thanks a lot

    << going to resume banging my head against a wall >>

    P.S. I tried converting all the values to text, but got the same results.
    Last edited by ANS; 07-31-2013 at 10:24 AM.

  2. #2
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Countif function goes crazy

    Can you post the actual formula?
    Or even better, a sample workbook that is displaying this behavior?


    FYI, a normal countif does not require CTRL + SHIFT + ENTER

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Countif function goes crazy

    Why did you enter the formula as an array formula?
    If posting code please use code tags, see here.

  4. #4
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Countif function goes crazy

    You don't need an array function (no cntrl+shift+enter) just to use countif.

    Can you upload an example? you could also just use a pivot table on the data table to show you the unique values and a count of the occurences..
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

  5. #5
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,697

    Re: Countif function goes crazy

    COUNTIF always interprets any text values that can be seen as numbers as those numbers, this can sometimes cause miscounting, e.g. if you have the text value

    1/1

    in A1 then this formula counts it

    =COUNTIF(A:A,"1/1/2013")

    because COUNTIF interprets 1/1 as a date, 1st jan in the current year - what sort of data are you counting?
    Audere est facere

  6. #6
    Registered User
    Join Date
    09-30-2012
    Location
    Tel-Aviv
    MS-Off Ver
    Excel 2010
    Posts
    84

    Re: Countif function goes crazy

    Hi all,
    thanks for the quick replies.

    * I was certain that with Countif(s) and Sumif(s) you MUST use ctrl+shift+enter. I'm surprised that's not the case. So, when do I and when don't I ?

    * Anyway, I got the exact same results with just Enter.

    * The data I'm working on is list of company names, so it's almost exclusively textual.
    It's also the reason why I can post my data, unfortunately


    Generally speaking, here is the formula:

    =COUNTIF('sheet with original data'!$B$2:$B$28124,'sheet after removing duplicates'!B2)

    the company names appear in the 2nd column on both.

    Thanks again all!

  7. #7
    Registered User
    Join Date
    09-30-2012
    Location
    Tel-Aviv
    MS-Off Ver
    Excel 2010
    Posts
    84

    Re: Countif function goes crazy

    Up until 10 mins ago I was certain countif requires it and will produce wrong numbers else.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Countif function goes crazy

    What happens if you create a pivot table from the original table with the company name as a row field and count of company name in the data section?

  9. #9
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,697

    Re: Countif function goes crazy

    Try using SUMPRODUCT like this

    =SUMPRODUCT(('sheet with original data'!$B$2:$B$28124='sheet after removing duplicates'!B2)+0)

    SUMPRODUCT is less efficient than COUNTIF but it has fewer inconsistencies - do you get the same results?

  10. #10
    Registered User
    Join Date
    09-30-2012
    Location
    Tel-Aviv
    MS-Off Ver
    Excel 2010
    Posts
    84

    Re: Countif function goes crazy

    Strange, I was certain I sent a reply.
    It seems to give the right numbers with a PT (Thanks for suggesting!), but I'm interested to understand what went wrong with the Countif I performed... for my general knowledge and for some more data manipulation I'm going to do on a subset of the data set at the next stage.
    Quote Originally Posted by Norie View Post
    What happens if you create a pivot table from the original table with the company name as a row field and count of company name in the data section?

  11. #11
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Countif function goes crazy

    Can you post a sample book that displays this behavior?

  12. #12
    Registered User
    Join Date
    09-30-2012
    Location
    Tel-Aviv
    MS-Off Ver
    Excel 2010
    Posts
    84

    Re: Countif function goes crazy

    Unfortunately, I can't. I know it's hard to answer questions without actually looking at the data...... and I appreciate you guys trying to help
    Quote Originally Posted by Jonmo1 View Post
    Can you post a sample book that displays this behavior?

  13. #13
    Registered User
    Join Date
    09-30-2012
    Location
    Tel-Aviv
    MS-Off Ver
    Excel 2010
    Posts
    84

    Re: Countif function goes crazy

    The SUMPRODUCT provides very similar results (I compared to the countif). The major difference is that the value countif 'counted' 3170 times is only counted once with the sumproduct.
    However, the numbers still don't match what the pivot table provides, for example. I still get a higher number than the total # of rows in the original data set.

    Quote Originally Posted by daddylonglegs View Post
    Try using SUMPRODUCT like this

    =SUMPRODUCT(('sheet with original data'!$B$2:$B$28124='sheet after removing duplicates'!B2)+0)

    SUMPRODUCT is less efficient than COUNTIF but it has fewer inconsistencies - do you get the same results?

  14. #14
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Countif function goes crazy

    ANS

    Can you not mock up a sample workbook?

  15. #15
    Registered User
    Join Date
    09-30-2012
    Location
    Tel-Aviv
    MS-Off Ver
    Excel 2010
    Posts
    84

    Re: Countif function goes crazy

    Is there a way to obfuscate the original data without changing its properties ?

    Quote Originally Posted by Jonmo1 View Post
    Can you post a sample book that displays this behavior?

  16. #16
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Countif function goes crazy

    Quote Originally Posted by ANS View Post
    The SUMPRODUCT provides very similar results (I compared to the countif). The major difference is that the value countif 'counted' 3170 times is only counted once with the sumproduct.
    And what value was that?
    Does it contain any wildcards like * or ?

    Countif uses wildcards, sumproduct does not

  17. #17
    Registered User
    Join Date
    09-30-2012
    Location
    Tel-Aviv
    MS-Off Ver
    Excel 2010
    Posts
    84

    Re: Countif function goes crazy

    I'm just concerned it will "work" just fine with any dummy data I generate specifically for this purpose.
    I'll try

    Quote Originally Posted by Norie View Post
    ANS

    Can you not mock up a sample workbook?

  18. #18
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Countif function goes crazy

    Just save another copy of the book for the forum, then you can obfuscate all day long and not alter the original book. :D

  19. #19
    Registered User
    Join Date
    09-30-2012
    Location
    Tel-Aviv
    MS-Off Ver
    Excel 2010
    Posts
    84

    Re: Countif function goes crazy

    Ha!
    It includes "<" !!
    Why is it counting it 3170 times??

    What are other wildcards and how will COUNTIF treat them?

    Countif will always give wrong results when there are characters such as * or ? in the cell value?

    Quote Originally Posted by Jonmo1 View Post
    And what value was that?
    Does it contain any wildcards like * or ?

    Countif uses wildcards, sumproduct does not

  20. #20
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Countif function goes crazy

    countif is treating the < literally as the Less than symbol

    so if the string in the cell is "<Hello there"
    Countif is reading it as "Less than Hello there"

    Any text string beginning with a letter lower than H, like Frank, Bill, Alice, will be counted.

  21. #21
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Countif function goes crazy

    The wild card would come into play if you had say

    John Doe
    John Smith
    John*

    Then the countif on John* would be counted 3 times.

  22. #22
    Registered User
    Join Date
    09-30-2012
    Location
    Tel-Aviv
    MS-Off Ver
    Excel 2010
    Posts
    84

    Re: Countif function goes crazy

    Holy Mozes!

    Is there a way to "fix" it so I could still use the countif function?

    What other wildcards should I watch out for?

    Thank you!

    Quote Originally Posted by Jonmo1 View Post
    countif is treating the < literally as the Less than symbol

    so if the string in the cell is "<Hello there"
    Countif is reading it as "Less than Hello there"

    Any text string beginning with a letter lower than H, like Frank, Bill, Alice, will be counted.

  23. #23
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Countif function goes crazy

    I think it's been clearly established that a pivot table is the best solution..


    I would recommend doing a find/replace on both the source data and the DeDuped data to remove < > * ? characters
    When doing the * and ? characters, use ~* and ~?, this makes it treat them as literal characters instead of wildcards.

  24. #24
    Registered User
    Join Date
    09-30-2012
    Location
    Tel-Aviv
    MS-Off Ver
    Excel 2010
    Posts
    84

    Re: Countif function goes crazy

    Pivot table would work great if you need no further analysis.
    If you do, it becomes cumbersome.

    Thanks so much for your help!

  25. #25
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Countif function goes crazy

    ANS

    How would the pivot prevent/hamper further analysis?

    If you really don't want the pivot then just copy the results from it, paste them somewhere and delete the pivot table.

  26. #26
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Countif function goes crazy

    Quote Originally Posted by ANS View Post
    Thanks so much for your help!
    You're welcome.

  27. #27
    Registered User
    Join Date
    09-30-2012
    Location
    Tel-Aviv
    MS-Off Ver
    Excel 2010
    Posts
    84

    Re: Countif function goes crazy

    You know how cumbersome pivot tables can be... so for instance if I wanted to have more columns with new data, calculations and manipulations, I would indeed need to copy paste all values. This may be an acceptable solution if you only perform the analysis once, but it's quite possible I'll add more records to the original list, etc. So I'll have to re-do the pivot table, copy & paste again, and so on.

    Thanks for the help again. I'm sorry I don't have enough reputation to go around!

    Quote Originally Posted by Norie View Post
    ANS

    How would the pivot prevent/hamper further analysis?

    If you really don't want the pivot then just copy the results from it, paste them somewhere and delete the pivot table.

  28. #28
    Registered User
    Join Date
    09-30-2012
    Location
    Tel-Aviv
    MS-Off Ver
    Excel 2010
    Posts
    84

    Re: Countif function goes crazy

    Can you please explain this formula to me? I thought sumproduct takes two arrays (?) of numbers and sums up the product of each two corresponding values.
    The way this formula is laid out, there are no 2 arrays, there's a "=" sign, and top top it all off, there's +0

    Would really appreciate some explanation.

    Quote Originally Posted by daddylonglegs View Post
    Try using SUMPRODUCT like this

    =SUMPRODUCT(('sheet with original data'!$B$2:$B$28124='sheet after removing duplicates'!B2)+0)

    SUMPRODUCT is less efficient than COUNTIF but it has fewer inconsistencies - do you get the same results?

  29. #29
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,697

    Re: Countif function goes crazy

    Yes, SUMPRODUCT normally multiplies 2 or more arrays/ranges and then sums the results but I used it here because it can normally be used without "array entry", so in fact you can use SUM if you want

    This part

    ='sheet with original data'!$B$2:$B$28124='sheet after removing duplicates'!B2

    returns an array of TRUE/FALSE values TRUE when the entry matches B2, FALSE when it doesn't

    If you then use some mathematical operation on that array which doesn't alter the value, like "double negation" --, or *1 or +0 as I used then that converts the TRUE/FALSE value to 1/0 values and if you sum those you effectively get a count of matches, so you could use

    =SUM(('sheet with original data'!$B$2:$B$28124='sheet after removing duplicates'!B2)+0)

    or with an IF function like

    =SUM(IF('sheet with original data'!$B$2:$B$28124='sheet after removing duplicates'!B2,1,0))

    ....but both of those (unlike the SUMPRODUCT version) require you to "array enter" the formula, i.e. confirm with CTRL+SHIFT+ENTER

  30. #30
    Registered User
    Join Date
    09-30-2012
    Location
    Tel-Aviv
    MS-Off Ver
    Excel 2010
    Posts
    84

    Re: Countif function goes crazy

    I think I kinddof understand Thanks!

    Quote Originally Posted by daddylonglegs View Post
    Yes, SUMPRODUCT normally multiplies 2 or more arrays/ranges and then sums the results but I used it here because it can normally be used without "array entry", so in fact you can use SUM if you want

    This part

    ='sheet with original data'!$B$2:$B$28124='sheet after removing duplicates'!B2

    returns an array of TRUE/FALSE values TRUE when the entry matches B2, FALSE when it doesn't

    If you then use some mathematical operation on that array which doesn't alter the value, like "double negation" --, or *1 or +0 as I used then that converts the TRUE/FALSE value to 1/0 values and if you sum those you effectively get a count of matches, so you could use

    =SUM(('sheet with original data'!$B$2:$B$28124='sheet after removing duplicates'!B2)+0)

    or with an IF function like

    =SUM(IF('sheet with original data'!$B$2:$B$28124='sheet after removing duplicates'!B2,1,0))

    ....but both of those (unlike the SUMPRODUCT version) require you to "array enter" the formula, i.e. confirm with CTRL+SHIFT+ENTER

+ 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. Multiple IF function (driving me CRAZY)
    By johnstonJr in forum Excel General
    Replies: 9
    Last Post: 07-10-2012, 05:56 PM
  2. Replies: 9
    Last Post: 07-02-2012, 07:02 PM
  3. Using Countif Function with the Month & Year Function
    By laya1024 in forum Excel General
    Replies: 3
    Last Post: 02-03-2009, 07:52 PM
  4. [SOLVED] You're Crazy
    By Lewis Clark in forum Excel General
    Replies: 2
    Last Post: 07-27-2006, 10:20 PM
  5. Embed a countif function in subtotal function?
    By Stuck at work in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-13-2006, 11:20 PM

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