+ Reply to Thread
Results 1 to 19 of 19

countif over multiple ranges?

  1. #1
    Registered User
    Join Date
    10-16-2008
    Location
    iowa
    MS-Off Ver
    Microsoft 365 Apps for enterprise
    Posts
    63

    countif over multiple ranges?

    I have a column range "data", and two tables (one for each area), and a named range of the cost row of each area;
    So I then have two named ranges; "data", and "cost", where "cost" name looks like this: =12:12,18:18

    Now I want to do some computations using countif and/or sumif, like this:
    countif(data cost, [criteria])
    and
    sumif(data dates, [criteria], data cost)

    (Similarly, "dates" are rows in each area)

    Both of these fail, but if I change to use the same functions just over each row intersection with "data" separately, it works fine.

    Thus, it seems like countif/sumif fail with a range which evaluates to multiple disjoint areas - yes?

    But it is cumbersome (messy) to have to write multiple copies of a formula(s) for each product, instead of just having a name which indicates the cost rows for all of them.

  2. #2
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: countif over multiple ranges?

    If you are not aware of it INDEX has a seldom used 4th argument called area_num.

    I've never nested it in any of the IF(s) functions; it's my first thought though.

    Perhaps a sample workbook file would make what you want to do clearer.

    If you are not familiar with how to do this:

    To attach a file to your post: (Please no pics or screenshots ... saves retyping data.),
    • be sure to desensitize the data
    • click “Go Advanced” (next to Post Quick Reply – bottom right),
    • scroll down until you see “Manage Attachments”, click that,
    • click “Browse”.
    • select your file(s)
    • click “Upload”
    • click “Close window”
    • click “Submit Reply”

    The file name will appear at the bottom of your reply.
    Dave

  3. #3
    Registered User
    Join Date
    10-16-2008
    Location
    iowa
    MS-Off Ver
    Microsoft 365 Apps for enterprise
    Posts
    63

    Re: countif over multiple ranges?

    Excel-countif.jpg
    Example attached.
    Attached Files Attached Files
    Last edited by guthrie; 07-12-2018 at 08:24 AM.

  4. #4
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,053

    Re: countif over multiple ranges?

    There was no sample attached. Will you please attach a SMALL sample Excel workbook (10-20 rows of data is usually enough)? Please don't attach a picture of one (no-one will want to re-type all your stuff before starting).

    1. Make sure that your sample data are truly REPRESENTATIVE of your real data. The use of unrepresentative data is very frustrating and can lead to long delays in reaching a solution.

    2. Make sure that your desired solution is also shown (mock up the results manually).

    3. Make sure that all confidential information is removed first!!

    4. Try to avoid using merged cells. They cause lots of problems!

    Unfortunately the attachment icon doesn't work at the moment. So, to attach an Excel file you have to do the following: Just before posting, scroll down to Go Advanced and then scroll down to Manage Attachments. Now follow the instructions at the top of that screen.
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU.

    Temporary addition of accented to illustrate ongoing problem to the TT: L? fh?ile P?draig sona dhaoibh

  5. #5
    Registered User
    Join Date
    10-16-2008
    Location
    iowa
    MS-Off Ver
    Microsoft 365 Apps for enterprise
    Posts
    63

    Re: countif over multiple ranges?

    Sorry - mistake in attaching - updated now.

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,853

    Re: countif over multiple ranges?

    Why the use of "data"?

    =SUMIF(days1,"<8",cost1)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  7. #7
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,053

    Re: countif over multiple ranges?

    Hi. I'm a wee bit confused about what you're trying to do. But.... Is this it??

    =SUMPRODUCT(--($B$8:$F$12<=6),($B$9:$F$13),($A$8:$A$12="Day")+N(T(LEN($B$8:$F$8))))
    Attached Files Attached Files

  8. #8
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: countif over multiple ranges?

    I don't know how you'd get it to work without the numbered ranges, but this should work with them:

    =SUM(SUMIF(INDIRECT("days"&{1,2}),"<8",INDIRECT("cost"&{1,2})))

  9. #9
    Registered User
    Join Date
    10-16-2008
    Location
    iowa
    MS-Off Ver
    Microsoft 365 Apps for enterprise
    Posts
    63

    Re: countif over multiple ranges?

    @Glen Kennedy: no; the answer is =280 (for your data; you changed the data from mine); the cost total for all days less than day 8 (in the real case, the day is a variable).

    There are lots of ways to do it, I gave a few - but the question is about the countif function; it's definition is that it works over ranges, but this example seems to show that it will not work over any range with disjoint parts.

    this is just a simple example to show this intended usage of countif, the question is about the function. I have seen similar undocumented range limitations on other excel functions, e.g. sumif seems to have a similar undocumented limitation.

  10. #10
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,053

    Re: countif over multiple ranges?

    In testing, I changed the formula

    =SUMPRODUCT(--($B$8:$F$12<=6),($B$9:$F$13),($A$8:$A$12="Day")+N(T(LEN($B$8:$F$8))))

    It should have been

    =SUMPRODUCT(--($B$8:$F$12<8),($B$9:$F$13),($A$8:$A$12="Day")+N(T(LEN($B$8:$F$8))))

  11. #11
    Registered User
    Join Date
    10-16-2008
    Location
    iowa
    MS-Off Ver
    Microsoft 365 Apps for enterprise
    Posts
    63

    Re: countif over multiple ranges?

    @rory1; Thanks, neat solution, but my overall goal was to have one place where I could add references to new tables with data; and other formulae (this is just one example) would all pickup the new data just from expanding the "days" & "cost" names.

    Your example is very nice, but I would have to locate every formula doing arithmetic over the tables and change all of them. So, so far it seems like the answer to my original question - can countif (and similar functions) work over named disjoint ranges, is "no". But given that, I like your work-around.

    As I noted elsewhere, I have seen this same limitation on other excel functions, and I wish they would document it, and thus be more precise (=correct) in their documentation.

  12. #12
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,853

    Re: countif over multiple ranges?

    In that case, it would seem you have answered your own question. Whilst it is not precisely documented, nowhere in the Office support for SUMIF or COUNTIF does it suggest that you can use it in the way you have tried to do so - all examples are given with single, one-dimensional arrays.

  13. #13
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: countif over multiple ranges?

    Quote Originally Posted by guthrie View Post
    it seems like the answer to my original question - can countif (and similar functions) work over named disjoint ranges, is "no".
    That is correct.

  14. #14
    Registered User
    Join Date
    10-16-2008
    Location
    iowa
    MS-Off Ver
    Microsoft 365 Apps for enterprise
    Posts
    63

    Re: countif over multiple ranges?

    @rorya: I like your examle, but the evaluation surprises me - #value in sums gets evaluated to a good number!?countif2.jpg

  15. #15
    Registered User
    Join Date
    10-16-2008
    Location
    iowa
    MS-Off Ver
    Microsoft 365 Apps for enterprise
    Posts
    63

    Re: countif over multiple ranges?

    @AliGW: Yes, I already answered it in the original post - I was just asking if that was correct(!).

    The definition /documentation/ for these says that they work over a range, and gives no restrictions on that. While one could say it is not precise, I think more precisely it is wrong, they do not work for any range, only certain specific limited types of ranges. Yes, no examples show this, since ... it doesn't work!

  16. #16
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,053

    Re: countif over multiple ranges?

    Bear in mind that INDIRECT is volatile. It recalculates every time something changes on your sheet. It can become VERY slow....

    You'll find that if you take account of the comment at Post 10, my suggestion works perfectly.

  17. #17
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: countif over multiple ranges?

    Quote Originally Posted by guthrie View Post
    @rorya: I like your examle, but the evaluation surprises me - #value in sums gets evaluated to a good number!?Attachment 581604
    The formula evaluation tool is limited, especially where array coercion is involved.

  18. #18
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,853

    Re: countif over multiple ranges?

    Quote Originally Posted by guthrie View Post
    @AliGW: Yes, I already answered it in the original post - I was just asking if that was correct(!).
    Yes, I know - and that is why I said you had answered your own question.

  19. #19
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,053

    Re: countif over multiple ranges?

    I have re-attached the file here. As you can see it works fine, even when extended ranges are used.... just remember thet the ranges are offset by one rowto sum the costs that are one row below the day
    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)

Similar Threads

  1. COUNTIF from an Array or multiple Ranges
    By Jossilyn in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-25-2017, 06:43 PM
  2. VBA Countif Multicreteria Multiple ranges
    By flavieng in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-16-2013, 03:38 PM
  3. [SOLVED] Something like COUNTIF but can take multiple ranges?
    By ablits1 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-09-2013, 12:37 PM
  4. [SOLVED] trying to do a "countif" with multiple ranges and multiple criteria. Countif, Sumproduct?
    By completelyhis in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-07-2013, 06:12 PM
  5. COUNTIF greater than, less than across multiple ranges
    By shetektn in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-24-2010, 10:43 PM
  6. Countif with multiple ranges
    By dr mint in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-20-2010, 02:14 AM
  7. COUNTIF() With multiple ranges and variables
    By Thansal in forum Excel General
    Replies: 3
    Last Post: 07-12-2006, 12:35 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