Results 1 to 6 of 6

Thread: Create Loop to Sum 5 cells, Enter Value in 5th Cell

  1. #1

    Create Loop to Sum 5 cells, Enter Value in 5th Cell



    Register for a FREE account, and/
    or Log in to avoid these ads!

    I unfortunately know almost nothing about VBA, so I'm really depending on you guys for help.

    I'm trying to write a code that, in a new column(AN), a) sums 5 cells in another column(AM), b)re-starts the loop at the end of 5 cells, c)uses the 5th cell as the 1st value in the next loop and d)enters the sum in the 5th cell of column (AN). To be clear, I'd like the sum of AM4:AM8 to be populated in AN8, and so on, with the next sum to pull AM8 as its first value. There are also FALSE statements in column (AM) A standard sum offset will populate straight down instead of offsetting again, so I need a vba code.


    I've attached the workbook. The values I want summed are in AM, and I'd like the sums to be in column AN.

    I don't know if this is possible or hard, but I would be so unbelievable appreciative of help!
    Attached Files Attached Files

  2. #2
    Administrator Ken Puls's Avatar
    Join Date
    Mar 2011
    Location
    Nanaimo, BC, Canada
    Posts
    2,268
    Articles
    57
    Blog Entries
    14
    Excel Version
    Excel Office 365 Insider
    Hi there,

    Give this a go and see if it does what you want. Copy the code into a standard module first. There is a link in my signature as to where to put it.

    Code:
    Sub SUM_every_fifth()
    
        Dim lSum As Double
        Dim lLast As Double
        Dim j As Long
        
        Do Until Selection.Value = vbNullString
            lSum = lLast
            For j = 1 To 5
                lSum = lSum + Selection.Value
                Selection.Offset(1, 0).Select
            Next j
            Selection.Offset(-1, 1).Value = lSum
            lLast = Selection.Offset(-1, 0).Value
        Loop
    End Sub
    Once you have it in there, select the first cell in the column of data, and run it.

    Not the prettiest code, but hopefully it does what you're after.
    Ken Puls, FCPA, FCMA, MS MVP

    Learn to Master Your Data at the Power Query Academy (the world's most comprehensive online Power Query training), with my book M is for Data Monkey, or our new Power Query Recipe cards!

    Main Site: http://www.excelguru.ca -||- Blog: http://www.excelguru.ca/blog -||- Forums: http://www.excelguru.ca/forums
    Check out the Excelguru Facebook Fan Page -||- Follow Me on Twitter

    If you've been given VBA code (a macro) for your solution, but don't know where to put it, CLICK HERE.

  3. #3
    Super Moderator JoePublic's Avatar
    Join Date
    Sep 2011
    Location
    Askew
    Posts
    183
    Articles
    0
    Excel Version
    2010
    Please also have a read of this: http://www.excelguru.ca/content.php?184
    and update your post to add links to all your cross-posts in other forums.
    Thanks.
    Circumference of a circle = 2πr²



    ²the circle's radius

  4. #4
    Here's the code that worked:

    Code:
    Sub Summate2():
    Worksheets("Calc").Activate
    Dim S As Double, r As Long, lr As Long, i As Integer
    lr = Worksheets("Calc").Range("CH" & Rows.Count).End(xlUp).Row
    For r = 4 To lr:
        For i = 0 To 4
            If IsNumeric(Worksheets("Calc").Range("CH" & r + i).Value2) Then S = S + Worksheets("Calc").Range("CH" & r + i).Value2
    Next i: r = r + i - 1
    If r > lr Then GoTo EndSub
    Worksheets("Calc").Range("CI" & r) = S: S = 0
    Next r
    EndSub: Worksheets("Calc").Range("CI" & lr) = S: End Sub

  5. #5
    How do I mark this as solved?

  6. #6
    Administrator Ken Puls's Avatar
    Join Date
    Mar 2011
    Location
    Nanaimo, BC, Canada
    Posts
    2,268
    Articles
    57
    Blog Entries
    14
    Excel Version
    Excel Office 365 Insider
    We don't mark threads as solved here, (one day I'll get around to adding that). Thanks for posting your solution back though, I appreciate it.
    Ken Puls, FCPA, FCMA, MS MVP

    Learn to Master Your Data at the Power Query Academy (the world's most comprehensive online Power Query training), with my book M is for Data Monkey, or our new Power Query Recipe cards!

    Main Site: http://www.excelguru.ca -||- Blog: http://www.excelguru.ca/blog -||- Forums: http://www.excelguru.ca/forums
    Check out the Excelguru Facebook Fan Page -||- Follow Me on Twitter

    If you've been given VBA code (a macro) for your solution, but don't know where to put it, CLICK HERE.

Tags for this Thread

Posting Permissions

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