Search code examples
arraysgoogle-sheetsarray-formulas

ArrayFormula in google sheets SUM if values match criteria


Ok, so I've been stuck on this for a while now trying to create an array formula that will do the following:

Let's pretend the data I'm working with appears as such:

         SHEET 1                                     SHEET 2
   |   A  |   B  |   C  |   D   |  E   | F        |   A   |   B  |
 1 |  Up  | Down | Down |   Up  | Down |        1 |  Red  | $50  |
 2 |  Red | Green| White| Black | Blue |        2 | Green | $100 |
 3 |  $50 | $200 | $15  | Hidden|      |        3 | White | $10  |
 4 |      | $200 |      | Hidden| $100 |        4 | Black | $70  |
 5 |  $50 | $200 | $15  | Hidden|      |        5 | Blue  | $100 |

Using that data, I need a formula in column F (that I can copy and paste for subsequent cells in the column).

My actual sheet has about 45 columns across and about 25 rows down with sensitive financial information, so I'm using pretend data above on a smaller scale.

So the formula should look something like this (this would be posted in column F of Sheet 1). It needs to check if $A$1:$E$1="Up" (disregard anything that is not "Up") in addition, IF that array = "up" then check to make sure $A3:$E3>$0 (I intend to exclude blank values and "hidden" values), then IF those 2 criteria are met, then return the corresponding value from $A$2:$E$2 and use that as a search key in a lookup formula (HLookup, Vlookup, offset+match, etc). So for example, the Formula in F3 would match those 2 criteria (which would be A2 [red] and then sum together all of the matching values - in this case, there is only 1 matching value, but usually there will be 3-5).

Attempt 1:

=ArrayFormula(SUM(OFFSET('Sheet 2'!$B$1,MATCH(IF(AND($A$1:$E$1="Up",$A3:$E3>"$0"),$A$2:$E$2,""),'Sheet 2'!$A$1:$A$5,0),0)))

Attempt 2:

=ArrayFormula(SUM(IF(AND($A$1:$E$1="Up",$A3:$E3>"$0"),OFFSET('Sheet 2'!$B$1,MATCH($A$2:$E$2,'Sheet 2'!$A$1:$A$5,0),0),"")))

My first attempt attempts to create an array for the Match search key that uses the IF statements to determine the results to be used in the match condition (In the example above, the only result would be "Red", but in the actual sheet, it could be an array like "Red, Blue, Yellow").

My second attempt attempts to first determine which cells we are using (based on IF conditions), then if the cell matches the IF then we will OFFSET the particular cell.

The errors I have noticed is that match doesn't seem to like my array in the search key. The first attempt results with an error saying it cannot find " in the range. (I guess I don't understand why it was searching for that...?). The 2nd attempt seems to fail because it uses the ENTIRE range ($A$2:$E$2), instead of only the cells that match the criteria.

I have tried such things as: VLookUp, HLookUp, LookUp (but it is not a sorted list). I've tried some other weird tactics, but non seem to give me what I need.

So far, the best I have come up with is: =ArrayFormula(SUM(IF($A$1:$E$1="Up",IF($A3:$E3>"$0",0,OFFSET('Sheet 2'!$B$1,MATCH($A2:$E2,'Sheet 2'!$A$1:$A$5,0),0)),0)))

In my sheet, it found 3 cells that matched the criteria, but instead of adding Cell 1 value, Cell 2 value, and Cell 3 value, it just added Cell 1 value 3 times. So it seems it was only using $A2 (the first option in the range for the match search key) and repeated it for every cell that matched.

I have also tried to include the Filter function (instead of IF) to eliminate cells that do not have "up" as their value ($A$1:$E$1).


Solution

  • Use SUMPRODUCT:

    =SUMPRODUCT(($A$1:$E$1="Up")*(VLOOKUP($A$2:$E$2,'Sheet 2'!$A$1:$B$5,2,FALSE))*($A3:$E3>0)*(ISNUMBER($A3:$E3)))