Question

I have data in Google Sheets organized like so:

Sheet1:

Label     Type
foo       B
bar       A
baz       _(empty cell)
qux       B

totalX    quantity
totalY    quantity

Sheet2:

Type    X      Y
A       2      5
B       1      3
C       3      4

I would like 'quantity' in Sheet1 to sum type-associated values from Sheet2. In this example, totalX would be 4 (1+2+1, or Bx+Ax+Bx), and totalY would be 11 (3+5+3, or By+Ay+By).

Conceptually, the formula in totalX could look at each cell in 'Type' column on Sheet1, then if that cell is not blank it finds the value on Sheet2 and adds it to the total. After playing with SUMIF and various formulas, I can't manage to figure out how best to do this.

Was it helpful?

Solution

Via http://igoogledrive.blogspot.com/2013/07/google-spreadsheet-advanced-vlookup-and.html:

To calculate totalX, if 'Label' and 'Type' are column A on their respective sheets:

 =sum(iferror(arrayformula(if(B2:B="";"";iferror(if(match(B2:B;'Sheet2'!A2:A4;0);vlookup(B2:B;'Sheet2'!A1:C4;{2}*sign(row(B2:B));false)))))))

And to calculate totalY:

 =sum(iferror(arrayformula(if(B2:B="";"";iferror(if(match(B2:B;'Sheet2'!A2:A4;0);vlookup(B2:B;'Sheet2'!A1:C4;{3}*sign(row(B2:B));false)))))))

Use ISERROR and IFERROR to ignore empty cells and cells that aren't found. VLOOKUP gets the actual values; {x}*sign references the appropriate column in Sheet2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top