automatically sums each 5 row and the rest of the line

kayza

Board Regular
Joined
Apr 29, 2015
Messages
61
Office Version
  1. 2007
Platform
  1. Windows
I have data that I want to add up automatically every fifth row and the rest.
here is The following data:
1
2
3
4
5
15
6
7
8
9
10
40
11
12???

<tbody>
</tbody>


code that I use is like this:
Code:
Dim x As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For x = 5 To Lastrow Step 5
        Range("B" & x).Value = Application.Sum(Range("A" & x & ":A" & x - 4))
    Next

from the code above, it will only sums each fifth line, but the rest of the line will not be added together.

how the correct code to solve this issues

Thank you in advance
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Is this what you need?
Code:
    Dim x As Long
    Dim Lastrow As Long
    Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For x = 5 To Lastrow Step 5
        Range("B" & x).Value = Application.Sum(Range("A" & x & ":A" & x - 4))
    Next
    Range("B" & Lastrow).Value = Application.Sum(Range("A1:A" & Lastrow)) - _
        Application.Sum(Range("B1:B" & Lastrow))
 
Upvote 0
thank you Tetra201. it works like a charm
issues solved
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,595
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top