+ Reply to Thread
Results 1 to 17 of 17

Is there a better way of writing this Sumproduct between range formula?

  1. #1
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Is there a better way of writing this Sumproduct between range formula?

    Hello,

    I am just wondering if this formula I've done could have been written in a different or better way.

    =SUMPRODUCT(--((A1>=0)*(A1<=4))+SUMPRODUCT(--((A2>=0)*(A2<=4))+SUMPRODUCT(--((A3>=0)*(A3<=4))+SUMPRODUCT(--((A4>=5)*(A4<=9))+SUMPRODUCT(--((A5>=5)*(A4<=9))+SUMPRODUCT(--((A6>=5)*(A6<=9))+SUMPRODUCT(--((A7>=5)*(A4<=9)))))))))


    Thank you
    Sans
    Last edited by sans; 11-07-2011 at 09:27 AM.

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

    Re: Is there a better way of writing this Sumproduct between range formula?

    You probably don't need the SUMPRODUCTs wrapping the formulas...

    i.e. (A1>=0)*(A1<=4)+(A2>=0)*(A2<=4)+(A3>=0)*(A3<=4), etc....
    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.

  3. #3
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Re: Is there a better way of writing this Sumproduct between range formula?

    Thank you. Is it also possible to achieve the same result with a countif formula?

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

    Re: Is there a better way of writing this Sumproduct between range formula?

    I don't think so since your comparisons are not uniform.

    for example if you were checking that range A1:A7 was between 0 and 4, then you could use COUNTIFS.

    e.g.

    =COUNTIFS(A1:A7,">=0",A1:A7,"<=4")

  5. #5
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Re: Is there a better way of writing this Sumproduct between range formula?

    I understand, thank you for the explanation. I am just trying to learn a little more each day. I am just wondering if there is anything excel can't do

  6. #6
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Re: Is there a better way of writing this Sumproduct between range formula?

    If the following values

    9 4 9 2 1 7 10

    are in the corresponding cells in the formula below, shouldn't the result be 3? The result I am getting is 0.


    =SUMPRODUCT((LS4>=5)*(LS4<=9))+((LS5>=5)*(LS5<=9))+((A3>=5)*(A3<=9))+((A4>=5)*(A4<=9))+((A5>=5)*(A4<=9))+((A6>=5)*(A6<=9))+((A7>=5)*(A4<=9))

    Sans

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

    Re: Is there a better way of writing this Sumproduct between range formula?

    I get 4..

    Also, you don't need SUMPRODUCT at all...

    i.e. your formula can be just....

    =(LS4>=5)*(LS4<=9)+(LS5>=5)*(LS5<=9)+(A3>=5)*(A3<=9)+(A4>=5)*(A4<=9)+(A5>=5)*(A4<9)+(A6>=5)*(A6<=9)+(A7>=5)*(A4<=9)

    It evaluates each condition and returns TRUE or FALSE.. then TRUE*TRUE gives 1 and all other combos give 0... each of these resultant 1's and/or 0's are then added together to give final result.

  8. #8
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Re: Is there a better way of writing this Sumproduct between range formula?

    Shouldn't the result be 3 in the case of 9 4 9 2 1 7 10 as only 3 numbers are between 5 and 9?

    I am getting 3 now if I use the formula in a new sheet but if I use it in the normal sheet I keep getting 0. Does it make any difference if the values 9 4 9 2 1 7 10 have formulas in them?

    Thanks
    Sans

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

    Re: Is there a better way of writing this Sumproduct between range formula?

    The combination of Conditions that give TRUE*TRUE are:

    (LS4>=5)*(LS4<=9)
    (A3>=5)*(A3<=9)
    (A6>=5)*(A6<=9)
    (A7>=5)*(A4<=9)

    assuming that L24:L25 is 9 4 respectively and that A3:A7 contain 9 2 1 7 10 respectively

    As long as the formula results are actually numbers (not text that look like numbers) then it should work.

  10. #10
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Re: Is there a better way of writing this Sumproduct between range formula?

    I understand the conditions of the formula but I keep getting 0 for some reason. I am attaching the example. In the normal workbook the cells are not in a range so I cannot use the COUNTIFS formula you have written above

    Thank you
    Sans
    Attached Files Attached Files

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

    Re: Is there a better way of writing this Sumproduct between range formula?

    The cells that have a green triangle in them are formatted as text... click the little popup icon (exclamation point in yellow triangle) and choose to convert to number

  12. #12
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Re: Is there a better way of writing this Sumproduct between range formula?

    Oops, the background error checking option was disabled in the Options section. Thanks NBVC

  13. #13
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Re: Is there a better way of writing this Sumproduct between range formula?

    I just saw that the error checking option was enabled in the original workbook but not in the example attached above.Now again I keep getting 0. It's probably something silly.

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

    Re: Is there a better way of writing this Sumproduct between range formula?

    Enter a 0 in any cell and copy it. Then select A1:G1 and right-click, pick Paste Special and select Add. This will convert the cells to Numbers, then go to Format and select General in the Number tab to ensure the format remains as General.

  15. #15
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Re: Is there a better way of writing this Sumproduct between range formula?

    Ok, I finally found the problem, but I don't know how to fix it. The numbers 9 4 9 2 1 7 10 were obtained by using the formula =RIGHT(LS4,1),....
    As soon as I deleted the formulas in those cells and simply wrote the numbers 9 4 9 2 1 7 10 the

    =(LS4>=5)*(LS4<=9)+(LS5>=5)*(LS5<=9)+(A3>=5)*(A3<=9)+(A4>=5)*(A4<=9)+(A5>=5)*(A4<9)+(A6>=5)*(A6<=9)+ (A7>=5)*(A4<=9)

    formula worked. Is there a way that this can be circumvented?

    Thanks
    Sans

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

    Re: Is there a better way of writing this Sumproduct between range formula?

    Use instead:

    =--RIGHT(LS4,1)

    the -- coerces the text string (returned from RIGHT function) to number.

  17. #17
    Valued Forum Contributor sans's Avatar
    Join Date
    10-19-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    550

    Re: Is there a better way of writing this Sumproduct between range formula?

    That did the trick! Thank you!!!

+ 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