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
 
Just about to take a look at this but immediately unsure
I need to identify the start of each set of ranks. Given by rank 1.
However, how can do that if there is the possibility of ties for rank 1 ?
If the succession of ranks for a model went 1,1,1,2,3,4,5,6.... how would we be able to determine whether that's
Three tied rank 1 as per 1,1,1,2,3 or a single returned rank 1 followed by a separate sequence starting with two tied 1's etc ????
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
@radeon187 I tihnk I've answered my own question.
Var2 is the primary lookup
Each set of ranks, is associated with a different, discrete model ID ?
If so then then tied rank 1 are not an issue
 
Upvote 0
Hi Snakehips, thanks. Let me have a think when my head is less full of cotton wool.
 
Upvote 0
This has driven me round the bend! I have been getting discrepancies and assuming errors in the function logic. Going round and round in circles and never seeing nor fixing it. Then I realise that your 308967 extract data is not a true match for the apparent equivalent in the full data set.

Hopefully the below function does the following.
Sums 5 consecutive Rank rows, if there are 5, ignoring the actual rank numbers where they duplicate for ties. So 1,2,3,4,5. or 1,2,2,4,5
Parameters are:
Var , the range holding the relevant Var2 number eg Q3
DatRng , the range of the data table
CNUm , the relevant Suffix for C1, C2, C3 etc. eg 1 , 2, 3
RRt , the root number for 5 ranks to be summed. eg 1. for 1,2,3,4,5 , 2 for 2,3,4,5,6. etc


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

Dim ModID As Variant
Dim SubTot As Long
Dim DatArry() As Variant
Dim c As Integer
DatArry = DatRng.Value  'Data range to array

CNum = CNum + 5 'set column for C1, C2, etc
c = 0
    For r = LBound(DatArry, 1) To UBound(DatArry, 1)
            If DatArry(r, 5) = Var And DatArry(r, 12) <= RRt + 4 Then  'have a look
            
                    If DatArry(r, 12) = 1 And c = 0 Then  'is ist rank 1
                        ModID = DatArry(r, 1) 'Note the Model
                        c = 1  'Inc c count
                        If RRt = 1 Then SubTot = DatArry(r, CNum)  'Sub total
                        GoTo Done
                    End If
                 
                    If DatArry(r, 1) = ModID Then  'Not first so check Model same
                        c = c + 1  'Inc c count
                        If c >= RRt And c < (RRt + 5) Then SubTot = SubTot + DatArry(r, CNum)  'Update sub tot
                    Else  'otherwise if not same Model it is start of new set
                        ModID = DatArry(r, 1) 'Note the new Model
                        c = 1
                        If RRt = 1 Then SubTot = DatArry(r, CNum)  'Update sub tot
                     
                    End If
                  
                    If c = RRt + 4 Then
                        CSUM = CSUM + SubTot
                        SubTot = 0
                    End If
            End If
Done:
     Next r
    
Set DatArrry = Nothing
End Function



Book1
ABCDEFGHIJKLMNOPQ
1ModelIdSkustockVar1Var2C1C2C3C4C5orderRank
293615311497642804901000001C1 1 to 5Var2 Lookup
39361531249764459554000001481308967
49361531349764459551000002
593615314497642804901000002
69361531549764459551000003
793615315497642804901001003
Full Data Set
Cell Formulas
RangeFormula
P3P3=CSUM(Q3,A1:L481500,1,1)


With nearly 5000,000 rows, It won't be the fastest formula to calculate but isn't too bad.
Check it out carefully.
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,746
Members
449,050
Latest member
excelknuckles

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