Consulting

Results 1 to 3 of 3

Thread: VBA Macro to add values by date

  1. #1
    VBAX Regular
    Joined
    Aug 2009
    Posts
    22
    Location

    VBA Macro to add values by date

    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.
    Capture.PNG

    Thanks

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,909
    Location
    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

  3. #3
    Knowledge Base Approver VBAX Guru
    Joined
    Apr 2012
    Posts
    4,822
    Use a pivottable.

    What's the benefit of this ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •