Sumif Provided Other Rows = x

andybason

Board Regular
Joined
Jan 7, 2012
Messages
217
Office Version
  1. 2016
Hi all,

I have a table (extract below) and I am using the following formula to sum the values in F where the Q3 criteria is met:

Code:
=SUMIF(E2:E77,$Q$3,F2:F77)

I would like to adapt it to only sum modelIDs that have a 1, 2, 3, 4 or 5 in range L2:L77.

Is this possible?

Thanks


Book1
ABCDEFGHIJKL
1modelIdSkustockVar1Var2C1C2C3C4C5orderRank
21034825114592572538020000001
31034825124592572538020000002
41034825134592572538024000003
51034825144592572538021000004
61034825162889922538021000105
71034825172889922538020000006
81034825182889922538020000007
91034825182889922538021001007
101034825192889922538020000009
11103482511028899225380240000010
12103482511128899225380200000011
13103482511228899225380210000012
14103482511345925725380210000013
15103482511745925725380210000014
1610348251191926425380240000015
1710348251201926425380240000016
1810348251211926425380200000117
1910348251221926425380210000018
2010348251241926425380210000019
21103482512528899225380210000020
22103482513028899225380210000021
2310348251311926425380210000022
2410348251351926425380210000023
25103482513731115825380210000024
26103482514031115825380210000025
27103482514231115825380210000026
281034825143891725380200000127
2910235951155143381800000001
3010235951165143381800000002
3110235951175143381800000003
3210235951185143381800000004
33102359512055585381800000005
34102359512155585381800000016
35102360521730825181801000001
36102360521977422381800000002
37102360522077422381802000003
38102360522177422381800000004
39102360522277422381800000005
40102360522377422381800000006
41102360522477422381800000017
42102361122744654881803000001
43102361123044654881800000002
441023611234594181800000013
4510236192391061781800000001
4610236192401061781801000002
4710236192421061781801000003
48102361924332662581801000004
49102361924532662581800000005
50102361924632662581806000006
51102361924732662581800000007
52102361924832662581800000008
5310236192501061781801000009
54102361925210617818000000010
55102361925310617818010000011
561023619260326625818010000012
5710236192608076818030003012
5810236192618076818040000014
5910236192628076818000000015
6010236192638076818010000016
6110236192658076818040000017
6210236192668076818060000018
631023619268326625818010000019
641023619270326625818010000020
651023619272326625818010000021
6610236192734864818010000022
6710236192784864818060000023
681023619281308798818000000024
691023619282308798818010000025
701023619284308798818010000026
7110236192854864818060000027
7210236192864864818000000028
7310236192874864818020000029
7410236192884864818060000030
7510236192894864818000000131
76102362923439866681804000001
77102362923539866681800000002
Sheet3
 
Andy, I see now that you are only wanting to sum complete sets of 1 to 5

I've looked at this and to be honest, I'm struggling to do it without a helper column.
Book1
ABCDEFGHIJKLMNOPQ
1ModelIdSkustockVar1Var2C1C2C3C4C5orderRankHelper
2103166524031559430896700000013
310316652413155943089671000002 6308967
410316652482305583089670000003 
510316652513155943089670000004 
610316652523155943089672000005 
710316652533155943089674000006 
810316652543155943089671000007 
910316652552305583089672000008 
1010316652562305583089670000009 
11103166525723055830896710000010 
12103166525923055830896710000011 
13103166526431559430896720000012 
14103166526531559430896720000013 
15103166526631559430896710000014 
16103166526723055930896720000015 
17103166526823055930896700000016 
18103166526923055930896700000017 
19103166527023055930896740000018 
20103166527123055930896700000019 
21103166527223055930896710000020 
22103166527345007530896700000021 
23103166527445007530896720000022 
24103166527545007530896710000023 
25103166527645007530896710010024 
26103166527745007530896710000025 
27103166528123055930896710000026 
28103166528323055930896710000027 
29103166528545007530896700000128 
30103482721023182030896710000013
31103482721036253833089670000002 
32103482721046253833089670000003 
33103482721056253833089671000004 
3410348272109318203089671000005 
3510348272112318203089674000006 
3610348272113318203089672000007 
3710348272114318203089676000008 
38103482721176253833089670000009 
391034827211862538330896700000110 
4010348292856253833089670000001 
4110348292866253833089674000012 
4210683001394730043089674000001 
Sheet15
Cell Formulas
RangeFormula
P3P3=SUM(N2:N42)
N2:N42N2=IF(AND(E2=$Q$3,L2=1,SUM(L2:L6)=15),SUM(F2:F6),"")


What is your resistance to a helper, given that you can have it out of the way of your data set / hidden if you wish?
The only other possibility , I reckon, would be using vba to create a UDF.
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi Snakehips,

Thanks for all your help with this. The problem with a helper column is that when the table is refreshed it deletes everything and repastes. This then either deletes additional columns or causes issues with formula references.
 
Upvote 0
Will you always be applying this to sum F, based on the model on in E and the ranking in L ?
Or, might you use D (Var1) and sum the C2, C3 or C4 columns?

Are you ok with a UDF ?
 
Upvote 0
Hi Snakehips,

I won't use Var1 but I may want to sum C2, C3 and C4.

A UDF would be ok.

Thanks
 
Upvote 0
Andy, give this a try.

Copy the below to a code Module
Parameters:
ModID is the target ModelID Cell Address
DatRng is the whole data range A1: L??
CNum is the numeric suffix of whichever 'C' column, C1, C2, C3, C4 or C5, is to be summed.

It does assume that ALL Rank sequences will begin with 1

VBA Code:
Function CSUM(ModID As Range, DatRng As Range, CNum As Integer) As Long

Dim SubTot As Long
Dim DatArry() As Variant
DatArry = DatRng.Value
CNum = CNum + 5
    For r = LBound(DatArry, 1) To UBound(DatArry, 1) - 4
        If DatArry(r, 12) = 1 And DatArry(r, 5) = ModID.Value And DatArry(r + 4, 12) = 5 Then
            For c = r To r + 4
                SubTot = SubTot + DatArry(c, CNum)
            Next c
            r = r + 4
        End If
        CSUM = CSUM + SubTot
        SubTot = 0
    Next r
Set DatArrry = Nothing

End Function

Book1
ABCDEFGHIJKLMNOPQ
1ModelIdSkustockVar1Var2C1C2C3C4C5orderRank
210316652403155943089670000001
310316652413155943089671090002C16308967
410316652482305583089670000003C341
5103166525131559430896700130004
610316652523155943089672000005
710316652533155943089674000006
810316652543155943089671000007
910316652552305583089672000008
1010316652562305583089670000009
11103166525723055830896710000010
12103166525923055830896710000011
13103166526431559430896720000012
14103166526531559430896720000013
15103166526631559430896710000014
16103166526723055930896720000015
17103166526823055930896700000016
18103166526923055930896700000017
19103166527023055930896740000018
20103166527123055930896700000019
21103166527223055930896710000020
22103166527345007530896700000021
23103166527445007530896720000022
24103166527545007530896710000023
25103166527645007530896710010024
26103166527745007530896710000025
27103166528123055930896710000026
28103166528323055930896710000027
29103166528545007530896700000128
3010348272102318203089671000001
311034827210362538330896700190002
32103482721046253833089670000003
33103482721056253833089671000004
3410348272109318203089671000005
3510348272112318203089674000006
3610348272113318203089672000007
3710348272114318203089673000008
38103482721176253833089670000009
391034827211862538330896700000110
4010348292856253833089670000001
4110348292866253833089674000012
4210683001396253833089674000001
Sheet15
Cell Formulas
RangeFormula
P3P3=CSUM(Q3,A1:L42,1)
P4P4=CSUM(Q3,A1:L42,3)
 
Upvote 0
Hi Snakehips,

Thanks very much for this. It works well when I tested it on extracts of the full table but when I use it on the full table something seems to go wrong.

I can't attach the workbook to this post so I've uploaded it here: Snakehips Extract.zip

I tested two Var2 numbers: 308967 and 44410. In the first tab are the results of the formula using the full data set. I then extracted the records for each Var2 number and tested the formula against these. It returned different results to the full extract. I went through each extract and manually calculated the expected results and these matched what the formula returned using the extracts.

Sorry to be a pain...
 
Upvote 0
Andy, I have had a look at the uploaded file.
As it stands, this is not easy. I soon realised that the full data set is not sorted. Hence, unlike the extracted examples you have given, the 1- 5 rank groups for the given Model are not consecutive in the data range. My original UDF assumed that they would be as per the examples. That accounts for the major discrepancy.

Once I realised that,I set about changing the UDF so that it would work on the full, unsorted, data. I had a UDF that I felt should work but for certain ranges, even on the two extracted lists, it returned an error. It took me ages to realise that the rankings are not discrete!!! There are tied ranks!! And, if the tied ranks come within the range 1-5 then based on my current understanding, it causes an issue unless the UDF is geared up to deal with it. Even in the extracted 308967 sheet there are about 7 tied (duplicate) ranks in range 1 - 5.
So, you need to confirm how you need to handle it.
For example, if there is a tied rank 2, so ranks are 1,2,2,4,5,6,7,8 you want the sum 1,2,2,3,4,5. or 1,2,2,3,4. or Ignore such a group all together, and only sum anything 1,2,3,4,5, or what ????????
 
Upvote 0
Hi Snakehips,

Sorry, I've been laid low with the flu.

To answer your question, I would like to sum the first 5 including ties, so that would be 1,2,2,3,4 in your example.

Ultimately, what I want to do is sum the first 5, then 2-6, then 3-7, etc. When I posted the original question my intention was to take the logic for 1-5 then replicate it myself for the others.

I really appreciate all your help and no problem if this is too time consuming.

Thanks and keep safe.
 
Upvote 0
Hi Andy, Sorry you've had the lurgi. Hope you feel and stay better.

If you had 1,2,2,3,4,4,5,... then would your 1 to 5 sum would be 1,2,2,3,4 2 to 6 sum 2,2,3,4,4 ?
Do you compute the ranks or are they downloaded?

I currently have a very tight schedule of staying indoors and twiddling my thumbs but, I will try and find a spare hour or two to see if we can crack this for you.
 
Upvote 0
Hi Snakehips,

Thanks. Crazy times.

Yes, the sums would be as you say.

The ranks are imported into Excel from a database.

Thanks
 
Upvote 0

Forum statistics

Threads
1,216,215
Messages
6,129,560
Members
449,516
Latest member
lukaderanged

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