PDA

View Full Version : VBA Macro to add values by date



riteoh
05-22-2020, 10:15 PM
I have an excel spreadsheet containing approx 100 records for each day over an 11 year period. For each day in the spreadsheet I need to take the value in a starting cell and add it to the value in the next cell, to get an accumulated total, recording these values in a third cell For example C2 = 1, C3=2, C4=3, C5=4.
I need to write a macro that then shows
D2=1, D3=3, D4=6, .....

Can anybody give me any ideas on how to do this? Refer the example below.
26726
Thanks

Kenneth Hobs
05-26-2020, 11:40 AM
Sub Main()
Dim r As Range, i As Long

Set r = Range("A2", Cells(Rows.Count, "A").End(xlUp))

For i = 1 To r.Count
r(i).Offset(, 3).Formula = "=" & r(i).Offset(, 2).Address & "+" & r(i - 1).Offset(, 3).Address
If r(i) <> r(i).Offset(-1) Then r(i).Offset(, 3) = r(i).Offset(, 2)
Next i
End Sub

snb
05-27-2020, 03:55 AM
Use a pivottable.

What's the benefit of this ?