Sample Data
Column A has numbers 1 to 5. Column B is empty and will hold the running sum calculated by VBA code using variables and loops.
| Cell | Value |
|---|---|
| A1 | Start |
| A2 | 1 |
| A3 | 2 |
| A4 | 3 |
| A5 | 4 |
| A6 | 5 |
| B1 | Sum |
| B2 | |
| B3 | |
| B4 | |
| B5 | |
| B6 |
Column A has numbers 1 to 5. Column B is empty and will hold the running sum calculated by VBA code using variables and loops.
| Cell | Value |
|---|---|
| A1 | Start |
| A2 | 1 |
| A3 | 2 |
| A4 | 3 |
| A5 | 4 |
| A6 | 5 |
| B1 | Sum |
| B2 | |
| B3 | |
| B4 | |
| B5 | |
| B6 |
Sub SumLoop()
Dim total As Integer
Dim i As Integer
total = 0
For i = 2 To 6
total = total + Cells(i, 1).Value
Cells(i, 2).Value = total
Next i
End SubA B 1 |Start |Sum | 2 | 1 | -> | 3 | 2 | -> | 4 | 3 | -> | 5 | 4 | -> | 6 | 5 | -> |
A B 1 |Start |Sum | 2 | 1 | 1 | 3 | 2 | 3 | 4 | 3 | 6 | 5 | 4 | 10 | 6 | 5 | 15 |