I am trying to add the values in column A for all rows that have the same value in column B. My code will sum but the values are incorrect, off by a value of 2 to 3. See attached spreadsheet.
Option Explicit
Sub calc()
Dim LastRow As Integer
Dim sum As Integer
Dim x As Integer
Dim i As Integer
LastRow = ActiveSheet.UsedRange.Rows.Count
sum = 0
x = 2
For i = 2 To LastRow
If Cells(i, "B") = Cells(i + 1, "B") Then
sum = sum + Cells(i, "A").Value
Else
Cells(x, "D").Value = sum + Cells(i, "A").Value
sum = 0
x = x + 1
End If
Next i
End Sub
Bookmarks