Calculate the first four cells based on condition

addy_89

New Member
Joined
Jul 9, 2014
Messages
21
ABCD
x16-03-20142
x16-03-20143
x16-03-20144
x16-03-20145
x16-03-20146
x17-03-20147
x17-03-20148
x17-03-20149
x17-03-201410
y17-03-20141

<colgroup><col><col><col span="2"></colgroup><tbody>
</tbody>


In the above example, i want to sum the first four cells where data in column a and column b is matching and after summing the first four values, i want excel to sum the rest of the values for the matching data in column in a and b which not necessary would be 4, but will be less. I have the written the array formula for this using count if and sumifs but that is giving me the sum of total values.

{=IF(COUNTIFS($A$2:$A3,A3,$B$2:$B3,B3)>1,"",SUMIFS(C:C,A:A,A3,B:B,B3))}

Is it possible? Please help, i'm stuck :(
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Works for me, took about 20 seconds to recalculate the sheet.
 
Upvote 0
Works for me, took about 20 seconds to recalculate the sheet.


Returns with error in d3, please see the elaborative example:-

S.NoABCD
1x3/16/20142SUMC1
2x3/17/20143SUMC2
3x3/18/20144SUMC3
4x3/19/20145SUMC4
5x3/20/20146SUMC5
6x3/21/20147SUMC6
7x3/21/20148BLANK
8x3/23/20149SUMC8
9x3/24/201410SUM(C9:12)
10x3/24/20141BLANK
11y3/24/20142BLANK
12y3/24/20142BLANK
13y3/24/20142SUMC13
14y3/26/20142SUMC14
15y3/26/20142BLANK

<colgroup><col span="2"><col><col><col></colgroup><tbody>
</tbody>


check
 
Upvote 0
The formula will not work if the data starts in row 1, if you don't have headers you will need to insert an empty row at the top.
 
Upvote 0
The only differences I can see between the formula results and your sample are S.No 9, 11 and 14, which are errors in your sample, not the formula. The dates are the same, but column A changes at S.No 11.
 
Upvote 0
I have a few comments/suggestions.


That won't work,..
That implies you haven't tried it. Whilst it is possible such a statement may be correct you should remember that people here are trying to help (for no physical reward) & a summary dismissal like that is not very encouraging.


It has headers,still not working
This at least says you have tried it. However, specific example of where it fails, what result it produces & what result it should have produced would help readers understand your requirement better.


Thank you for your help jackson,
Again, given that people are trying to help, a little more care with their name wouldn't hurt. ;)


Works for me, took about 20 seconds to recalculate the sheet.
Jason, I agree that your formulas produce the results the OP has described, as opposed to the sample provided in post #13. However, you must have a better machine than me as with 250,000 rows, Excel stopped responding and/or told me I had insufficient resources to complete the task.


The sheet below shows Jason's formulas in column D and an alternative I tried in column E. I did get a little further with my formula but the calculation still eventually stalled on a large sample. I show the example so that specific rows/values can be referred to if the results are not what is required.

Excel Workbook
ABCDE
1JPS
2x16/03/14777
3x17/03/1451717
4x17/03/146
5x17/03/143
6x17/03/143
7x17/03/1471515
8x17/03/141
9x17/03/147
10x18/03/14777
11x19/03/14111
12x20/03/14444
13x21/03/1482020
14x21/03/148
15x21/03/144
16x21/03/149
17x21/03/1461414
18x21/03/148
19x23/03/14111
20x24/03/1491313
21x24/03/144
22y24/03/1451313
23y24/03/147
24y24/03/141
25y26/03/1461111
26y26/03/145
27
Sheet3



With so many rows, I suspect any formula solution will cause significant sheet performance issues, so I offer the following macro as an alternative. For me it processed 290,000 rows in about half a second.
Rich (BB code):
Sub SumInFours()
  Dim a, b
  Dim rws As Long, i As Long, k As Long, z As Long
  Dim t As Double

  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 3).Value
  rws = UBound(a, 1)
  ReDim b(1 To rws, 1 To 1)
  k = 1
  t = a(1, 3)
  For i = 2 To rws
    If z = 3 Or a(i, 1) <> a(i - 1, 1) Or a(i, 2) <> a(i - 1, 2) Then
      b(k, 1) = t
      t = a(i, 3)
      k = i
      z = 0
    Else
      t = t + a(i, 3)
      z = z + 1
    End If
  Next i
  b(k, 1) = t
  Range("D2").Resize(rws).Value = b
End Sub
 
Upvote 0
I have a few comments/suggestions.


That implies you haven't tried it. Whilst it is possible such a statement may be correct you should remember that people here are trying to help (for no physical reward) & a summary dismissal like that is not very encouraging.


This at least says you have tried it. However, specific example of where it fails, what result it produces & what result it should have produced would help readers understand your requirement better.


Again, given that people are trying to help, a little more care with their name wouldn't hurt. ;)

You said what I was thinking Peter.

Peter_SSs said:
Jason, I agree that your formulas produce the results the OP has described, as opposed to the sample provided in post #13. However, you must have a better machine than me as with 250,000 rows, Excel stopped responding and/or told me I had insufficient resources to complete the task.

Peter, I could, and should have given a better response, but given the level of discouragement that you noted throughout the thread, I wasn't exactly motivated to do so at the time.

When the OP gave the size of the dataset a vba solution was my immediate thought, but, given the above, I chose to push my existing solution.

When I tried the formula with 250k rows, I did get the "insufficient resources" message after autofilling, however, checking the sheet the formula had filled the entire range as expected and forcing a full recalc revealed no further issues. Maybe the autofill was right on the limit of my machines capability :confused:

Hopefully one of the 3 working solutions we have provided will be acceptable for the OP.
 
Upvote 0
Peter,

I've just tried your formula with 250k rows to compare system performance.

Autofill returned insufficient resources with only 11k rows calculated. Forced recalc I cancelled after about a minute at only 8% complete :eek:
 
Upvote 0
Just missed the edit timeout again :oops:

I remembered an observation from an old thread that countif(s) and sumif(s) seem to be extremely inefficient when filled over a large range using an absolute start and relative end reference.

With this in mind, I cannibalized your formula and came up with

=IF(MOD(COUNTIFS(INDEX(A$1:A2,MAX(ROW(D2)-4,2)):A2,A2,INDEX(B$1:B2,MAX(ROW(D2)-4,2)):B2,B2),4)=1,SUMIFS(C2:C5,A2:A5,A2,B2:B5,B2),"")

250k rows took about 2-3 seconds, and no insufficient resource failures.
 
Upvote 0

Forum statistics

Threads
1,215,406
Messages
6,124,720
Members
449,184
Latest member
COrmerod

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