Help with SUM formula based on data in separate table

dogpile

New Member
Joined
Oct 24, 2005
Messages
21
Could I get some assistance on a formula that I need in cell B11 below, where I'm looking to sum the total in column B for all of the units that are categorized as "old" (i.e. what's in cell A11) based on the table in F1:H7.


Excel 2013 64 bit
ABCDEFGH
1Location1Location2Location1Location2
2Unit 11,00015,000Unit 1oldnew
3Unit 21,00015,000Unit 2newnew
4Unit 32,5002,000Unit 3newnew
5Unit 43,000Unit 4oldnew
6Unit 4100Unit 5newnew
7Unit 51,5004,000Unit 6oldnew
8Unit 6500800
9Unit 6500600
10
11old
Sheet1


Thanks in advance!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try:

=SUM(SUMIF($A$2:$A$9,IF(G$1:G$7="old",$F$1:$F$7,"xx"),B$2:B$9))

confirmed with Control+Shift+Enter.
 
Upvote 0
Brilliant, thank you. Can I add another criteria to the formula?
If column D is "cr" then include the negative of that amount (reverse sign) in the sum.



Excel 2013 64 bit
ABCDEFGHI
1Location1Location2Location1Location2
2Unit 11,00015,000drUnit 1oldnew
3Unit 21,00015,000crUnit 2newnew
4Unit 32,5002,000crUnit 3newnew
5Unit 43,000crUnit 4oldnew
6Unit 4100crUnit 5newnew
7Unit 51,5004,000drUnit 6oldnew
8Unit 6500800dr
9Unit 6500600dr
10
11old
Sheet2
 
Last edited:
Upvote 0
Well, by just modifying the previous formula, this would work:

=SUM(SUMIFS(B$2:B$9,$A$2:$A$9,IF(G$1:G$7="old",$F$1:$F$7,"xx"),$D$2:$D$9,"dr"))-SUM(SUMIFS(B$2:B$9,$A$2:$A$9,IF(G$1:G$7="old",$F$1:$F$7,"xx"),$D$2:$D$9,"cr"))

with CSE.

But on further reflection, this works as well and is shorter:

=SUM(IF(COUNTIFS($F$2:$F$7,$A$2:$A$9,$G$2:$G$7,"old"),$B$2:$B$9*IF($D$2:$D$9="cr",-1,1)))

with CSE.
 
Upvote 0
Now if only my computer doesn't sound like it's going to take off when I apply this formula to the much larger data I'm try to sum... it can't handle it!
 
Upvote 0
I don't know how to improve the efficiency of the formulas much more beyond what is there. You could try a UDF.

On a copy of your workbook, right click on the sheet tab on the bottom and select "View Code". From the VBA Editor menu bar, click Insert > Module. On the window that opens, paste this code:

Code:
Public Function SumSome(ByVal Tab1 As Range, ByVal Tab2 As Range, ByVal Tab3 As Range, ByVal Tab4 As Range, ByVal MyCode As String)
Dim MyData1 As Variant, MyData2 As Variant, MyData3 As Variant, MyDict As Object, r As Long

    If Tab1.Columns.Count > 1 Or _
       Tab2.Columns.Count > 1 Or _
       Tab3.Columns.Count > 1 Or _
       Tab4.Columns.Count <> 2 Or _
       Tab1.Rows.Count <> Tab2.Rows.Count Or _
       Tab1.Rows.Count <> Tab3.Rows.Count Then
       SumSome = "Invalid ranges in parameters"
       Exit Function
    End If
    
    MyData1 = Tab4.Value
    Set MyDict = CreateObject("Scripting.Dictionary")
    For r = 1 To UBound(MyData1)
        MyDict(MyData1(r, 1)) = MyData1(r, 2)
    Next r
    
    MyData1 = Tab1.Value
    MyData2 = Tab2.Value
    MyData3 = Tab3.Value
    For r = 1 To UBound(MyData1)
        If MyDict(MyData1(r, 1)) = MyCode Then SumSome = SumSome + IIf(MyData3(r, 1) = "cr", -1, 1) * MyData2(r, 1)
    Next r
    
End Function
Now go back to your Excel sheet and enter this formula:

=SumSome(A2:A9,B2:B9,D2:D9,F2:G7,"old")

That might work faster. Good luck!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top