3
votes

I recently came to know about Ctrl+Shift+Enter array formulas of Excel and currently still learning them. Coming to my problem,

SheetA:
Product        Code
S1             19875
S2             19834
S1             13575
S1             35675
S2             47875   



SheetB:
Code           Indent
19875          40  
19834          15
13575          22
35675          25
47875          20

I need to do Sum of all Indents of a given Product name.

Eg: I need S1's Total Indent,

  • Vlookup on SheetA, Get the Code 19875
  • Perform a vlookup on SheetB, Get the Indent of 40
  • Next Vlookup on Sheet A, Get the code 13575
  • Use 13575 to Vlookup on SheetB, Get Indent of 22
  • Next Vlookup on Sheet A, Get the code 35675
  • Use 35675 to Vlookup on SheetB, get indent of 25
  • Sum of 40+22+25, Return 87

I can achieve this through VBA, but I'm wondering if this is possible within excel functions using CSE/Array formulas.

Edit:

I don't have values in Sheet2 in the same order of Sheet1.. They are completely random. My SheetB will be something randomly like following:

SheetB:
Code           Indent
19834          40  
19875          15
47875          22
13575          25
35675          20
5

5 Answers

7
votes
{=SUM(NOT(ISNA(MATCH((($A$2:$A$6="S1")*(B2:B6)),Sheet2!$A$2:$A$6,FALSE)))*(Sheet2!$B$2:$B$6))}

The first argument of the MATCH resolves to

{19875;0;13575;35675;0}

The MATCH resolves to

{1;#N/A;3;4;#N/A}

You'll have to make sure you don't have zeros in SheetB. The NOT ISNA turns those into TRUEs and FALSEs and resolves to

{TRUE;FALSE;TRUE;TRUE;FALSE}

And the final SUM looks like this

=SUM({TRUE;FALSE;TRUE;TRUE;FALSE}*{40;15;22;25;20})

Update

I can't figure out a single-array solution when the lists are in a different order. My attempts with OFFSET and TRANSPOSE either gave the wrong answer or crashed Excel. If you can stand using a helper column, you could put this formula in third column of your first sheet

=VLOOKUP(B2,Sheet2!$A$2:$B$6,2,FALSE)

and then use this array formula to sum them up

{=SUM(($A$2:$A$6=A2)*($C$2:$C$6))}
0
votes

If the Code column were identical (same order) on both SheetA and SheetB, a simple SUMIF function would do. Similarly, if the INDENT data were on SheetA, you could also use a pivot table to calculate the sums quickly.

I'm guessing the design of your workbook won't allow this though. In which case, I don't have any easy solutions, but I'll give it another look.

0
votes

You could use the below: //Ola.S

Table 1 Col:A B C Product Code Intendent

Col C: =SUMIF($E$4:$E$8;B4;$F$4:$F$8)

Table 2 Col:E F Code Intendent

Table 3 Pivottable (Col:A and C)

0
votes

On sheetC If A1 has S1 then in Cell B1 type

=SUM((IF((Product=A1),Code1)=TRANSPOSE(Code2))*TRANSPOSE(Amt))

Array entered Where Code1 = Dynamic Range for Codes on SheetA starting from Row2 Code2 = Dynamic Range for Codes on SheetB starting from Row2

0
votes

No TRANSPOSE calls needed.

Try the array formula

=SUM(SUMIF(SheetB!A2:A6,IF(SheetA!A2:A6="S1",SheetA!B2:B6),SheetB!B2:B6))

Assuming no cell in SheetB!A2:A6 it evaluates to FALSE. If the Code columns were all numeric (probably a questionable assumption), this could be done without an array entry using:

=SUMPRODUCT(SUMIF(SheetB!A2:A6,SheetA!B2:B6/(SheetA!A2:A6="S1"),SheetB!B2:B6))

Assuming no cell in SheetB!A2:A6 evaluates to #DIV/0!.