+ Reply to Thread
Results 1 to 7 of 7

array function with SUM

  1. #1
    Registered User
    Join Date
    08-25-2011
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    4

    array function with SUM

    Hello all, first time poster here.

    I have output from a query that looks like this (simplified version):

    1 2 3 4
    1 100 200 300 400
    2 500 600 700 800
    3 900 100 200 300
    4 400 500 600 700

    I want to use an array formula to sum certain parts of the data, and I want to be able to look at the top row. This is the formula I'm thinking of, but it returns #VALUE!:

    {=SUM((OFFSET($H$22:$K$25,20-ROW($H$22:$K$25),0)<=I20)*($H$22:$K$25))}

    I wanted to use offset() on each of the 16 cells, make it look at the top row, and see if the number is less than 2 (that's what cell I20 is).

    The output of the offset() function is {1;0;0;0}. It apparently starts at the cell with 100, grabs 1 (one cell above it), then looks 2, 3, and 4 cells above it.I want the offset function to output this (when using F9 in the formula):

    {=SUM(({1,2,3,4;1,2,3,4;1,2,3,4;1,2,3,4;}<=I20)*($H$22:$K$25))}

    Using that (when manually typed in) will give the desired output.


    Am I approaching this the wrong way? Thanks for the help!

  2. #2
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: array function with SUM

    You lost me on "Hello"
    So in your example, you would expect it to sum the first column (as that is the only column where the first row is < 2)?
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    08-25-2011
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: array function with SUM

    Quote Originally Posted by ChemistB View Post
    You lost me on "Hello"
    So in your example, you would expect it to sum the first column (as that is the only column where the first row is < 2)?
    I would want it to sum the first two columns in this example, so <= 2.

    Thanks for the reply.

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

    Re: array function with SUM

    If the top row is H21:K21 then try this formula

    =SUM(IF(H21:K21<=I20,H22:K25))

    confirmed with CTRL+SHIFT+ENTER

    ....or as long as all values in H22:K25 are numeric you can avoid CSE and use SUMPRODUCT with

    =SUMPRODUCT((H21:K21<=I20)*H22:K25)
    Audere est facere

  5. #5
    Registered User
    Join Date
    08-25-2011
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: array function with SUM

    Quote Originally Posted by daddylonglegs View Post
    If the top row is H21:K21 then try this formula

    =SUM(IF(H21:K21<=I20,H22:K25))

    confirmed with CTRL+SHIFT+ENTER

    ....or as long as all values in H22:K25 are numeric you can avoid CSE and use SUMPRODUCT with

    =SUMPRODUCT((H21:K21<=I20)*H22:K25)
    Thanks!

    I realized that I didn't include something. In my example, I eventually want to look at the top row (doing <= 2) and the leftmost column ( maybe >= 3 for instance).

    I can't use an index match formula, since that only returns one value - I want to sum up many values, such as the bottom-left area of numbers: 900, 100, 400, and 500.

    Many thanks.

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

    Re: array function with SUM

    You can use the same principle to look at both the top row and leftmost column, e.g. assuming leftmost column is G22:G25 you can use this array formula

    =SUM(IF(H21:K21<=2,IF(G22:G25>=3,H22:K25)))

    confirmed with CTRL+SHIFT+ENTER

    ....that will sum all values in H22:K25 where both conditions are met for header row and leftmost column.

    ....or SUMPRODUCT for the same result....

    =SUMPRODUCT((H21:K21<=2)*(G22:G25>=3),H22:K25)

  7. #7
    Registered User
    Join Date
    08-25-2011
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: array function with SUM

    Quote Originally Posted by daddylonglegs View Post
    You can use the same principle to look at both the top row and leftmost column, e.g. assuming leftmost column is G22:G25 you can use this array formula

    =SUM(IF(H21:K21<=2,IF(G22:G25>=3,H22:K25)))

    confirmed with CTRL+SHIFT+ENTER

    ....that will sum all values in H22:K25 where both conditions are met for header row and leftmost column.

    ....or SUMPRODUCT for the same result....

    =SUMPRODUCT((H21:K21<=2)*(G22:G25>=3),H22:K25)
    WOW, that worked! And yet it was so simple compared to what I was trying to do.

    I seriously owe you a beer, or at the very least, $20. Thanks!

+ 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