+ Reply to Thread
Results 1 to 6 of 6

Proper syntax to shorten Countif formula

  1. #1
    Registered User
    Join Date
    09-29-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    9

    Proper syntax to shorten Countif formula

    Hi, I have written the following COUNTIF formula which works, but is long and confusing. Is there any way of shortening and refining the rule? In essence an "error" is to be displayed if any sport is selected more than twice over C2:F2. I keep getting bracket errors if I try to shorten it.

    My formula:

    =IF(COUNTIF(C2:F2,"Football")>2,"error",IF(COUNTIF(C2:F2,"Fitness")>2,"error",IF(COUNTIF(C2:F2,"Badminton")>2,"error",IF(COUNTIF(C2:F2,"Rugby")>2,"error",""))))
    Last edited by small_wonder; 09-30-2012 at 05:14 PM.

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: Proper syntax to shorten Countif formula

    its not that much shorter, but try this...
    =IF(OR(COUNTIF(C2:F2,"Football")>2,COUNTIF(C2:F2,"Fitness")>2,COUNTIF(C2:F2,"Badminton")>2,COUNTIF(C2:F2,"Rugby")>2),"error","")
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

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

    Re: Proper syntax to shorten Countif formula

    Try this "array formula"

    =IF(MAX(COUNTIF(C2:F2,C2:F2))>2,"error","")

    confirmed with CTRL+SHIFT+ENTER
    Audere est facere

  4. #4
    Registered User
    Join Date
    09-29-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Proper syntax to shorten Countif formula

    Thank you FDibbins and daddylonglegs. That's much better. For now I will use FDibbins as I'm not sure how/why the C2:F2,C2:F2 works? I need to understand before I use... although it is super short! Can you please idiot proof daddylonglegs?

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

    Re: Proper syntax to shorten Countif formula

    COUNTIF expects a single value in the criteria part like

    =COUNTIF(Range,Criteria)

    If the criteria is a range like

    =COUNTIF(C2:F2,C2:F2)

    Then the result is an array showing 4 values in this case because C2:F2 is 4 cells, the count of C2 values in C2:F2, the count of D2 values in C2:F2, the count of E2 values in C2:F2 and the count of F2 values in C2:F2, so if you had the following in those cells

    Apple
    Banana
    Banana
    Banana

    =COUNTIF(C2:F2,C2:F2) gives you {1,3,3,3}

    so taking the MAX gives you 3.....which corresponds to the maximum number of repeated items in C2:F2.

    Of course my formula doesn't check specifically the sports you mention but I assume they are the only likely contents of those cells

  6. #6
    Registered User
    Join Date
    09-29-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Proper syntax to shorten Countif formula

    Thank you daddylonglegs, I knew how to use Min, Max and Average already.... I know who to contact when I return to university (masters)

+ 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