+ Reply to Thread
Results 1 to 17 of 17

Count with OR statement

  1. #1
    Registered User
    Join Date
    05-01-2012
    Location
    Greece
    MS-Off Ver
    Excel 2010
    Posts
    52

    Count with OR statement

    Hello. I am dealing with the following situation. I have columns A,B and C with 10 rows, say, and the cells contain values from 1 to 20. I would like to count in how many rows the value 1 (respectively 2,3,...,20) appears at least in one column.

    Any help appreciated.

  2. #2
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Count with OR statement

    Hi Καλή Χρονιά.

    One way could be this.

    In a cell(let's say H1) type the number that you want to count(or create a validation list to choose)

    In D1 and copy down put this formula.

    =COUNTIF(A1:C1;$H$1) Column D will be a helper and hidden column.

    Then use tis formula to get your result.

    =COUNTIF(D1:D10;">=1")
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

    --Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

  3. #3
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Count with OR statement

    Assuming your data is in the range A2:C11.

    =SUMPRODUCT(--(MMULT(--(A2:C11=1),{1;1;1})>0))
    Biff
    Microsoft MVP Excel
    Keep It Simple Stupid

    Let's Go Pens. We Want The Cup.

  4. #4
    Registered User
    Join Date
    05-01-2012
    Location
    Greece
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Count with OR statement

    At first thanks for your answers. I gave a try to make it work in a different way but it didn't work. Here is what I did:

    Assuming that the data where in the range A2:C11 as Tony Valko said, I named open1 the shells A2:A11, open2 the shells B2:B11 and open3 the shells C2:C11 from the name manager.

    Then, in order to count the 1s (note that 1 appeared in E2 at a column reference with the values 1,2,...,20), I wrote
    Please Login or Register  to view this content.
    . It returned #VALUE! with the message "A value used in the formula was of the wrong data type".

    Any ideas of what could have been wrong?

    Thank you again.

  5. #5
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Count with OR statement

    If you're wanting to count the number of ROWS where each cell contains a 1 then try it like this:

    =COUNTIFS(open1,E2,open2,E2,open3,E2)

  6. #6
    Registered User
    Join Date
    05-01-2012
    Location
    Greece
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Count with OR statement

    Quote Originally Posted by Tony Valko View Post
    If you're wanting to count ...
    I want to count the number of ROWS where at least one cell contains a 1

  7. #7
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Count with OR statement

    You really comfused me.

    I get(yesterday i think) a reb * from you and in your comment was this.


    Thread: Count with OR statement

    Worked. Thanks!
    What changed?

  8. #8
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Count with OR statement

    One way:

    =SUMPRODUCT(SIGN((open1=E2)+(open2=E2)+(open3=E2)))

  9. #9
    Registered User
    Join Date
    05-01-2012
    Location
    Greece
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Count with OR statement

    I don't understand what makes you think that something changed. What you proposed worked for me so I thanked you and voted your answer. I just wanted to find a second (third actually after Tony Valko's solution) way of doing that, using the OR statement, as I had tried in the first place. Is something wrong with that?

  10. #10
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Count with OR statement

    Quote Originally Posted by Tony Valko View Post
    Assuming your data is in the range A2:C11.

    =SUMPRODUCT(--(MMULT(--(A2:C11=1),{1;1;1})>0))
    Just a follow-up to add some info...

    The array {1;1;1} correlates to the number of columns in the data range.

    A2:C11 = 3 columns thus the array {1;1;1}.

  11. #11
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Count with OR statement

    Quote Originally Posted by akotronis View Post
    I don't understand what makes you think that something changed. What you proposed worked for me so I thanked you and voted your answer. I just wanted to find a second (third actually after Tony Valko's solution) way of doing that, using the OR statement, as I had tried in the first place. Is something wrong with that?
    In the way that you say...the answer is:

    NO. There is not something with that..

  12. #12
    Registered User
    Join Date
    05-01-2012
    Location
    Greece
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Count with OR statement

    Thanks once again Tony Valko. I notice that your first formula (with the mmult) works with sum instead of sumproduct too, but your last formula (with the sign) doesn't. I wonder why this happens.. I also wonder why neither
    Please Login or Register  to view this content.
    nor
    Please Login or Register  to view this content.
    work neither with sum nor with sumproduct and furthermore, why

    Please Login or Register  to view this content.
    and
    Please Login or Register  to view this content.
    give different results, since removing sumproduct, writing A2 B2 C2 instead of open1 open2 open3 respectively in a cell and copying down to 9 more cells the same array is produced in either of the two cases...

  13. #13
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Count with OR statement

    =SUM(IF((open1=$E$2)+(open2=$E$2)+(open3=$E$2);1;0))
    That one will work in you enter it as an array formula.

    Array formulas need to be entered using the key
    combination of CTRL,SHIFT,ENTER (not just ENTER).
    Hold down both the CTRL key and the SHIFT key
    then hit ENTER.

    =SUM(IF(OR((open1=1);(open2=1);(open3=1));1;0))
    That one won't work because the OR function returns a single result. There might be several rows that meet the criteria but the OR function will evaluate down to a single TRUE and the final result of the entire formula will be 1.

    Same thing with this one:

    =SUMPRODUCT(SIGN(OR((open1=E2);(open2=E2);(open3=E2))))

  14. #14
    Registered User
    Join Date
    05-01-2012
    Location
    Greece
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Count with OR statement

    Quote Originally Posted by Tony Valko View Post
    Please Login or Register  to view this content.
    That one will work in you enter it as an array formula.
    Indeed it worked!

    Yet another question (I hope the last one). I try to understand in which cases the symbol + acts as an OR statement and when it acts as summation.
    In
    Please Login or Register  to view this content.
    acted as an OR statement. But in
    Please Login or Register  to view this content.
    (where it was entered as an array formula and E2 has the value 1 to be counted in the way it was described in the first place) acted as summation, as the result was the total number of 1s appearing in the range A2:C11.

    I have an example here from a book that has the range A2:C11 with the columns A,B,C named Office Amount and Difference respectively and in order to sum the values in the Amount range that meet both of the criteria

    1) The value corresponding to the Difference range is negative
    2) The text corresponding to the Office range is Oregon or California

    it uses the Array formula

    Please Login or Register  to view this content.
    I try to understand what is the difference that would make this last formula work but
    Please Login or Register  to view this content.
    not..

  15. #15
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Count with OR statement

    Let's take a look at very small simplified example...

    A...B...C
    1...1...1

    Open1 = column A
    Open2 = column B
    Open3 = column C

    E2 = 1

    This array formula correctly returns 1:

    =SUM(IF((open1=$E$2)+(open2=$E$2)+(open3=$E$2);1;0))
    open1=$E$2 = TRUE
    open2=$E$2 = TRUE
    open3=$E$2 = TRUE

    So, the result of:

    (open1=$E$2)+(open2=$E$2)+(open3=$E$2) = 3

    The IF function evaluates the number 3 as a TRUE logical value. In Excel logical tests typically evaluate to TRUE or FALSE but some functions like the IF function will also evaluate ANY number other than 0 as TRUE and 0 as FALSE.

    So we have:

    SUM(IF(TRUE,1))

    SUM(1) = 1

    The other formula:

    =SUM(((open1=$E2)+(open2=$E2)+(open3=$E2))*1)
    Evaluates to:

    open1=$E2 = TRUE
    open2=$E2 = TRUE
    open3=$E2 = TRUE

    (open1=$E2)+(open2=$E2)+(open3=$E2) = 3

    SUM((3*1)) = 3

    SUM(3) = 3

    The best way to see how a formula works is to use the formula auditing tool, evaluate formula.

    Select a cell that contains a formula
    Goto the Formulas tab
    Formula Auditing
    Evaluate Formula
    Repeatedly clicking the Evaluate button will step through the formula evaluation process until Excel arrives at the final result.

    I use this quite often so I've put this button on one of my toolbars.

    One thing to do if you use this tool, use it on small samples. You don't want to use it with a formula that references entire columns. After you've tried it few times the reason for this will be obvious!

  16. #16
    Registered User
    Join Date
    05-01-2012
    Location
    Greece
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Count with OR statement

    Ok, now this cleared things a lot. Thank you very much for your time!!

  17. #17
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Count with OR statement

    You're welcome. Thanks for the feedback!

+ 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