+ Reply to Thread
Results 1 to 19 of 19

Nested OR Statement?

  1. #1
    Registered User
    Join Date
    10-24-2013
    Location
    Ontario, Canada
    MS-Off Ver
    2007
    Posts
    13

    Nested OR Statement?

    In simplest terms... I have two columns of numbers and I want to count the number of rows containing a number >0 in either of the two columns. I would like to create one formula to complete this calculation rather than adding a third column with an OR statement and a CountIF "true" total at the bottom (which would provide the correct answer).

    I would greatly appreciate any input.

    Thanks.
    Last edited by Caoimhe; 10-24-2013 at 07:33 PM. Reason: Solved

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

    Re: Nested OR Statement?

    One way...

    Data Range
    A
    B
    C
    D
    1
    ---
    ---
    2
    -18
    -56
    5
    3
    -16
    -83
    4
    -4
    45
    5
    -31
    -89
    6
    38
    22
    7
    15
    -37
    8
    16
    -42
    9
    -100
    -26
    10
    10
    -95

    This formula entered in D2:

    =SUMPRODUCT(SIGN((A2:A10>0)+(B2:B10>0)))
    Biff
    Microsoft MVP Excel
    Keep It Simple Stupid

    Let's Go Pens. We Want The Cup.

  3. #3
    Forum Guru AlKey's Avatar
    Join Date
    07-20-2009
    Location
    Lakeland, FL USA
    MS-Off Ver
    Microsoft Office 2010/ Office 365
    Posts
    8,903

    Re: Nested OR Statement?

    Or this:

    A
    B
    C
    D
    1
    2
    1
    0
    5
    =SUM(COUNTIF(A2:A5,">0"),COUNTIF(B2:B5,">0"))
    3
    2
    2
    4
    0
    2
    5
    3
    0
    If you like my answer please click on * Add Reputation
    Don't forget to mark threads as "Solved" if your problem has been resolved

    "Nothing is so firmly believed as what we least know."
    --Michel de Montaigne

  4. #4
    Registered User
    Join Date
    10-24-2013
    Location
    Ontario, Canada
    MS-Off Ver
    2007
    Posts
    13

    Re: Nested OR Statement?

    Most excellent! That will work. If you care to elaborate with a breakdown on how the formula works I'd be happy to learn however I am thrilled to have an answer so promptly. Thank you very much!

  5. #5
    Registered User
    Join Date
    10-24-2013
    Location
    Ontario, Canada
    MS-Off Ver
    2007
    Posts
    13

    Re: Nested OR Statement?

    This second formula does not work for me as I believe it provides a sum of the cells >0 rather than the sum of the rows with a cell value >0. (If that makes sense.)

  6. #6
    Forum Guru AlKey's Avatar
    Join Date
    07-20-2009
    Location
    Lakeland, FL USA
    MS-Off Ver
    Microsoft Office 2010/ Office 365
    Posts
    8,903

    Re: Solved: Nested OR Statement?

    The COUNTIF formula is actually is rather descriptive. It counts cells with filled with numbers, and in this case that are greater than 0. It looks for values in two columns A and B and summarizes the total count of none-zero values.

  7. #7
    Forum Guru AlKey's Avatar
    Join Date
    07-20-2009
    Location
    Lakeland, FL USA
    MS-Off Ver
    Microsoft Office 2010/ Office 365
    Posts
    8,903

    Re: Nested OR Statement?

    Quote Originally Posted by Caoimhe View Post
    This second formula does not work for me as I believe it provides a sum of the cells >0 rather than the sum of the rows with a cell value >0. (If that makes sense.)
    No, it provides the count.

  8. #8
    Registered User
    Join Date
    10-24-2013
    Location
    Ontario, Canada
    MS-Off Ver
    2007
    Posts
    13

    Re: Nested OR Statement?

    Sorry, yes, it provides the count of the cells >0 rather than the count of the rows with a cell value >0. My mistake. I was looking for a total in which a row with two values >0 is only counted once.
    Tony Valko's formula above works for my purpose.

  9. #9
    Forum Guru AlKey's Avatar
    Join Date
    07-20-2009
    Location
    Lakeland, FL USA
    MS-Off Ver
    Microsoft Office 2010/ Office 365
    Posts
    8,903

    Re: Solved: Nested OR Statement?

    They both do the same thing Since cells with 0 would have the same count as rows with 0. Simple.

  10. #10
    Registered User
    Join Date
    10-24-2013
    Location
    Ontario, Canada
    MS-Off Ver
    2007
    Posts
    13

    Re: Solved: Nested OR Statement?

    No, it's very different as there are two columns of data. If all data is >0 the total I need would be equal to the number of rows of data and only half of the number of cells of data.

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

    Re: Nested OR Statement?

    Here goes...

    =SUMPRODUCT(SIGN((A2:A10>0)+(B2:B10>0)))

    These expressions will return arrays of either TRUE or FALSE:

    A2:A10>0 and B2:B10>0... it will look like this (T = TRUE, F = FALSE):

    A2>0 = F ... B2>0 = F
    A3>0 = F ... B3>0 = F
    A4>0 = F ... B4>0 = T
    A5>0 = F ... B5>0 = F
    A6>0 = T ... B6>0 = T
    A7>0 = T ... B7>0 = F
    A8>0 = T ... B8>0 = F
    A9>0 = F ... B9>0 = F
    A10>0 = T ... B10>0 = F

    These arrays are then added together. TRUE gets coerced to 1 and FALSE gets coerced to 0.

    F + F = 0
    F + F = 0
    F + T = 1
    F + F = 0
    T + T = 2
    T + F = 1
    T + F = 1
    F + F = 0
    T + F = 1

    This array is then passed to the SIGN function. The SIGN function returns the mathematic sign of the numbers in the array.

    Negative numbers = sign -1
    0 = sign 0
    Positive numbers = sign 1

    So:

    0 = sign 0
    0 = sign 0
    1 = sign 1
    0 = sign 0
    2 = sign 1
    1 = sign 1
    1 = sign 1
    0 = sign 0
    1 = sign 1

    This array is then passed to the SUMPRODUCT function and summed to get the final formula result:

    =SUMPRODUCT({0;0;1;0;1;1;1;0;1}) = 5

    So, to count how many rows contain at least 1 number greater than 0:

    =SUMPRODUCT(SIGN((A2:A10>0)+(B2:B10>0)))

    =5

  12. #12
    Forum Guru AlKey's Avatar
    Join Date
    07-20-2009
    Location
    Lakeland, FL USA
    MS-Off Ver
    Microsoft Office 2010/ Office 365
    Posts
    8,903

    Re: Solved: Nested OR Statement?

    Oh, I see what you saying now.

    Try this

    =SUM(SIGN(COUNTIF(A2:A5,">0")),COUNTIF(B2:B5,">0"))

  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: Solved: Nested OR Statement?

    They want to count how many rows have at least 1 number greater than 0.

    Using this sample data:

    Sheet1
    A
    B
    2
    -18
    -56
    3
    -16
    -83
    4
    10
    45
    5
    -31
    -89

    The correct result is 1.

    Here's another way to do it:

    =SUMPRODUCT(--(MMULT(--(A2:B5>0),{1;1})>0))

  14. #14
    Registered User
    Join Date
    10-24-2013
    Location
    Ontario, Canada
    MS-Off Ver
    2007
    Posts
    13

    Re: Solved: Nested OR Statement?

    Alright Tony, that second formula looks even more confusing than the first. Thank you very much for the walk through of your first solution, I understand it now, that was very helpful.

  15. #15
    Registered User
    Join Date
    10-24-2013
    Location
    Ontario, Canada
    MS-Off Ver
    2007
    Posts
    13

    Re: Solved: Nested OR Statement?

    Quote Originally Posted by AlKey View Post

    =SUM(SIGN(COUNTIF(A2:A5,">0")),COUNTIF(B2:B5,">0"))
    Sorry, that doesn't quite work, although it's similar to Tony Valko's first formula (which seems to work without issue).
    Last edited by Caoimhe; 10-24-2013 at 09:32 PM. Reason: Correction

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

    Re: Solved: Nested OR Statement?

    Yeah, that's why I usually suggest something like the 1st formula.


    You're welcome. Thanks for the feedback!

  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: Solved: Nested OR Statement?

    =SUM(SIGN(COUNTIF(A2:A5,">0")),COUNTIF(B2:B5,">0"))
    No, that doesn't do the same thing as the formulas I suggested.

    If you get the correct result I think it's just a coincidence.

  18. #18
    Registered User
    Join Date
    10-24-2013
    Location
    Ontario, Canada
    MS-Off Ver
    2007
    Posts
    13

    Re: Solved: Nested OR Statement?

    Quote Originally Posted by Tony Valko View Post
    Here's another way to do it:

    =SUMPRODUCT(--(MMULT(--(A2:B5>0),{1;1})>0))
    Yes, that will do it as well, thank you. In my situation your first formula is prefered as the two columns required for the formula are not adjacent in my table.

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

    Re: Solved: Nested OR Statement?

    Good deal. 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)

Similar Threads

  1. [SOLVED] Nested IF Statement
    By frogz2564 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 07-24-2013, 01:48 AM
  2. nested IF statement
    By nigec in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 06-01-2013, 11:15 PM
  3. [SOLVED] Nested If Statement - ISBLANK and ISNA in same statement?
    By Janc in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 04-23-2013, 09:00 AM
  4. Nested OR Statement
    By Supdem in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 07-19-2010, 03:19 AM
  5. Replies: 6
    Last Post: 01-14-2009, 06:59 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