+ Reply to Thread
Results 1 to 15 of 15

OR() nested in COUNTIFS()

  1. #1
    Registered User
    Join Date
    11-26-2011
    Location
    Seattle, USA
    MS-Off Ver
    Excel 2011
    Posts
    17

    OR() nested in COUNTIFS()

    Hello,

    I'm having difficulties nesting an OR clause in a COUNTIFS function. The attached document presents a short, stylized version of some of the data in Sheet 1. In Sheet 2, I'm trying to compile counts of the data by Cell type, with some conditions. Any ideas on how to succeed would be very helpful.

    Thanks,

    Hillargi
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,064

    Re: OR() nested in COUNTIFS()

    Try this

    in B3
    =SUMPRODUCT((Sheet1!$A$3:$A$19=$A3)*((Sheet1!$B$3:$E$19="d")+(Sheet1!$B$3:$E$19="f")+(Sheet1!$B$3:$E$19="g")))
    in C3
    =SUMPRODUCT((Sheet1!$A$3:$A$19=$A3)*((Sheet1!$B$3:$E$19="f")+(Sheet1!$B$3:$E$19="g")+(Sheet1!$B$3:$E$19="k")))
    in D3
    =SUMPRODUCT((Sheet1!$A$3:$A$19=$A3)*((Sheet1!$B$3:$E$19="g")+(Sheet1!$B$3:$E$19="j")))
    and copy down
    Regards
    Special-K

    Ensure you describe your problem clearly, I have little time available to solve these problems and do not appreciate numerous changes to them.

  3. #3
    Registered User
    Join Date
    11-26-2011
    Location
    Seattle, USA
    MS-Off Ver
    Excel 2011
    Posts
    17

    Re: OR() nested in COUNTIFS()

    Hello and thank you for your response. Two main things, here:

    1. The SUMPRODUCT function you provided works well with few "OR" statements, and the idea of using it with logical operators gives me fodder for other projects. I also like that it works with cell references, as in:

    Please Login or Register  to view this content.
    instead of the constants you suggested:

    Please Login or Register  to view this content.
    Cell references are much more flexible for my purposes, as I may need to change location code formats in the future. I also like that I am able to apply this function to unbounded arrays (unbounded rows, at least), as in:

    Please Login or Register  to view this content.
    This allows the count to continue as new rows are added to the data on the primary sheet.

    2. However, the SUMPRODUCT function you provided is inefficient (or at least tiresome) to implement when there are many "OR" statements, since the data_array (in this case, Sheet1!$B:$E) has to be called separately for each statement.

    So, is there a way to improve this within the SUMPRODUCT function? Also, and perhaps more important, I'm still interested in making COUNTIFS work, for this and future projects. Is there a way to make the following (original) code work?

    Please Login or Register  to view this content.

    Many thanks,

    Hillargi

  4. #4
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: OR() nested in COUNTIFS()

    Countifs won't allow different width ranges as far as I know...



    Try:

    =SUMPRODUCT((Sheet1!$A$1:$A$17=Sheet2!$A3)*(ISNUMBER(MATCH(Sheet1!$B$1:$E$17,Sheet1!$G$2:$I$2,0))))

    adjust ranges to suit, but don't use ranges that are much large than necessary.

    This counts number of rows that have a match to A3 in column A and a match in B1:E17 of any G2:I2 value (the row is counted once even if multiple matches in B:E are found)>
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

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

    Re: OR() nested in COUNTIFS()

    Quote Originally Posted by NBVC View Post
    ......the row is counted once even if multiple matches in B:E are found....
    That isn't true is it? Although you are looking at a 17 cell range the formula can return a value up to 68.

    I'm not clear what Hillargi requires - should each row count only once a most of can it count more than that if you have multiple matches in B:E ?
    Audere est facere

  6. #6
    Registered User
    Join Date
    11-26-2011
    Location
    Seattle, USA
    MS-Off Ver
    Excel 2011
    Posts
    17

    Re: OR() nested in COUNTIFS()

    Interesting about the width ranges. Technically, then, this should work:

    Please Login or Register  to view this content.
    But it doesn't. So, what's wrong with that nested OR()? For that matter, why isn't this possible?

    Please Login or Register  to view this content.
    Hillargi

  7. #7
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: OR() nested in COUNTIFS()

    You can't use OR - this works as a viable formula to achieve a similar effect

    =SUMPRODUCT(COUNTIFS(Sheet1!$A:$E,Sheet2!$A3,Sheet1!$A:$E,Sheet1!$G$2:$I$2)))

    .....but I don't think that'll be much good to you because both ranges are the same so that can only return a non-zero value if Sheet2A3 is equal to one of Sheet1!$G$2:$I$2

    COUNTIFS returns an array of three values (one for each of Sheet1!$G$2:$I$2)

    and SUMPRODUCT is simply used to sum that 3 element array

  8. #8
    Registered User
    Join Date
    11-26-2011
    Location
    Seattle, USA
    MS-Off Ver
    Excel 2011
    Posts
    17

    Re: OR() nested in COUNTIFS()

    Quote Originally Posted by daddylonglegs View Post
    I'm not clear what Hillargi requires - should each row count only once a most of can it count more than that if you have multiple matches in B:E ?
    I would like the results from this code:

    Please Login or Register  to view this content.
    But without have to repeat Sheet1!$B:$E for each OR statement in the formula. Moreover, I would like to know why the following code does not work.

    Please Login or Register  to view this content.
    Hillargi

  9. #9
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: OR() nested in COUNTIFS()

    Quote Originally Posted by daddylonglegs View Post
    That isn't true is it? Although you are looking at a 17 cell range the formula can return a value up to 68.
    you are right dll, I miscounted in my small test

  10. #10
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: OR() nested in COUNTIFS()

    Quote Originally Posted by Hillargi View Post
    I would like the results from this code:

    Please Login or Register  to view this content.
    But without have to repeat Sheet1!$B:$E for each OR statement in the formula.
    Check out NBVC's suggestion - like that one it can count the same row multiple times if there are multiple matches in the same row within columns B:E, is that what you want to do?

    I believe I answered your second point in my last post........

  11. #11
    Registered User
    Join Date
    11-26-2011
    Location
    Seattle, USA
    MS-Off Ver
    Excel 2011
    Posts
    17

    Re: OR() nested in COUNTIFS()

    Quote Originally Posted by daddylonglegs View Post
    Check out NBVC's suggestion - like that one it can count the same row multiple times if there are multiple matches in the same row within columns B:E, is that what you want to do?

    I believe I answered your second point in my last post........
    You definitely answered my second question--case of concurrent writing, I suppose.
    Two things I still don't understand:
    1. What are these character limitations: 17? 68?
    2. The code works for unbounded columns, as in:
    Please Login or Register  to view this content.
    However, what would the formula look like in cell Sheet2!D3, when you're calling Sheet1!$I$2 and Sheet1!$K$2, which are not side by side (ie, can't use Sheet1!$I$2:Sheet1!$K$2) ?

    Many thanks for your continued patience.

    Hillargi

  12. #12
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: OR() nested in COUNTIFS()

    If whatever is in J2 won't be found in B:E, then you can use same formula without harm.. otherwise I think you will need to resort back to your original formula....

  13. #13
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: OR() nested in COUNTIFS()

    NBVC's example formula uses just 17 rows - I was pointing out that the formula could return a number up to 68 (one for every cell in Sheet1!$B$1:$E$17)

    You can use whole columns but that is one of the factors that makes the formula slow to calculate - COUNTIFS looks at the "used range" only so when you reference the whole column it's "clever" enough to look at just 200 rows if that's all you have used. SUMPRODUCT, however, doesn't do that so in Excel 2011 it will look at 1 million + rows, better then to perhaps restrict the range to 1,000 rows or 10,000, whatever will be sufficient to cover all your future data expansion.

    For non-contiguous criteria ranges you could simply put in the values like

    =SUMPRODUCT((Sheet1!$A:$A=Sheet2!$A3)*ISNUMBER(MATCH(Sheet1!$B:$E,{"g","j"},0)))

    or you can use CHOOSE like this

    =SUMPRODUCT((Sheet1!$A:$A=Sheet2!$A3)*ISNUMBER(MATCH(Sheet1!$B:$E,CHOOSE({1,2},Sheet1!I2,Sheet1!K2),0)))

    Note that {1,2} changes depending on the number of criteria cells, if you have 4 it needs to be {1,2,3,4}

  14. #14
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: OR() nested in COUNTIFS()

    I didn't think of the CHOOSE() option.. nice daddylonglegs

  15. #15
    Registered User
    Join Date
    11-26-2011
    Location
    Seattle, USA
    MS-Off Ver
    Excel 2011
    Posts
    17

    Re: OR() nested in COUNTIFS()

    Thanks again for the help. I was able to patch something together thanks to your insights, but still had problems accommodating many conditions that were not adjacent.

    Sincerely,

    Hillargi

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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