PDA

View Full Version : [SOLVED:] Cumuative Sum



DougR21
08-13-2021, 06:49 AM
Hi! I'm trying to code a macro to perform a cumulative sum. The starts is in the cell D1 and the first value is the cell A1 to the value of cell A2, and adds up to the value of cell C1. The problem is that when I run this macro it seems to me that it is in an infinite loop. Below is the macro code:

Sub CumSum ()

Dim W As Worksheet
Dim Min As Integer
Dim Max As Integer
Dim Cum As Integer

Set W = Sheets("Plan1")
Min = W.Range("A1").Value
Max = W.Range("A2").Value
Cum = W.Range("B1").Value
W.Range("D1").Value = Min
W.Select
W.Range("D1").Select

Do Until Min = Max
Min = Cum + ActiveCell.Value
ActiveCell.Offset(1, 0).Select
Loop

End Sub

And a worksheet screenshot with the values in the cells for example:

https://i.ibb.co/McPRb3C/Imagem1.png (https://imgbb.com/)

If anyone can point out the error to me, how to fix it, and if there is more efficient code for this macro's function, I would be immensely grateful.

anish.ms
08-13-2021, 12:22 PM
Is this what you are looking for?


Sub CumSum()


Dim W As Worksheet
Dim Min As Long, Max As Long, Cum As Long, i As Long, n As Long


Set W = Sheets("Plan1")

With W
Min = .Range("A1").Value
Max = .Range("A2").Value
Cum = .Range("B1").Value

n = 1
For i = Min To Max Step Cum
.Cells(n, 4) = i
n = n + 1
Next i
End With
End Sub

DougR21
08-13-2021, 06:14 PM
It works! Thanks a lot, man!!!