Consulting

Results 1 to 3 of 3

Thread: Cumuative Sum

  1. #1
    VBAX Newbie
    Joined
    Aug 2021
    Posts
    3
    Location

    Cumuative Sum

    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:



    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.

  2. #2
    VBAX Mentor
    Joined
    Nov 2020
    Location
    Cochin, Kerala
    Posts
    314
    Location
    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

  3. #3
    VBAX Newbie
    Joined
    Aug 2021
    Posts
    3
    Location
    It works! Thanks a lot, man!!!

Posting Permissions

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