title: Conditional Cumulative Sum Macro
body: <p>Hello I have been stuck on my first subroutine for a while now and have tried researching my problem but am fairly new to VBA so have had little luck finding anything specific. I have a rather large spreadsheet (56090 rows) which is full of daily data points for a bunch of items with unique names. The item name is located in column D and the data I need is located in columns F and H. I am trying to create a cumulative sum for each day in column K that resets when it reaches a new item name (by checking the current row item name with the previous row item name). The code I have seems to do the trick but stops running at row 22510 (no error, just the sum values after this row are wrong).</p>
  
  <pre><code>Sub cum(ByVal DataPoints As Long)
  Application.ScreenUpdating = False
  Dim index As Long
  Dim runningCum As Long
  
  runningCum = 0
  
  For index = 3 To DataPoints
  'Set cell to Cum Oil column at index row
      cel = Cells(index, "K")
      'If the well name in previous row is equal to well name in index row, add oil values to runningCum
      If Cells(index - 1, "D") = Cells(index, "D") Then
          runningCum = runningCum + Cells(index, "F") + Cells(index, "H")
      'Otherwise, reset runningCum
      Else
          runningCum = 0
      End If
      'Set the Cum Oil cell value to the runningCum
      cel = runningCum
  Next
  
  Application.ScreenUpdating = True
  End Sub
  </code></pre>