Running Total

Agzil

New Member
Joined
Apr 15, 2016
Messages
2
Hello All,

I'm still very new to vba, pardon my misgivings!

I was wondering how to modify this code if I wanted to add a running total of column A on column B rows every time I add a value on column A.
P.S. I'm tucking this to a user-form button.

ABC
1CoinTotalDemand
2223
324
415
538
619
7211
8213

<tbody>
</tbody>



Code:
Sub Test()
Dim Coin As Long
Dim Total As Long
Dim Demand As Long


Demand = Cells(2, 3)


Total = 0


For Coin = 2 To Cells(Rows.Count, 1).End(xlUp).Row
    
    Total = Total + Cells(Coin, 1)
    If Total > Demand Then
        Cells(Coin, 1).Interior.ColorIndex = 6
        Total = 0
    Else
        Cells(Coin, 1).Interior.ColorIndex = xlNone
    End If
Next Coin
End Sub

I appreciate all the help!
Thank you
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I am sure there are other ways to accomplish this, but I am still learning the more in depth areas of VBA, but you can do the following and it should work (I also added this to the "Worksheet_SelectionChange" so anytime someone changes something it runs automatically").

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim inTotal As Integer, inCoin As Integer
Dim Coin As Long, Total As Long, Demand As Long
Dim TotallastRow As Long, CoinlastRow As Long

TotallastRow = Range("B" & Rows.Count).End(xlUp).Row
CoinlastRow = Range("A" & Rows.Count).End(xlUp).Row

inTotal = Cells(TotallastRow, 2).Value
inCoin = Cells(CoinlastRow, 1).Value

If TotallastRow < CoinlastRow Then
    Cells(TotallastRow + 1, 2) = inCoin + inTotal
End If

Demand = Cells(2, 3)


Total = 0


For Coin = 2 To Cells(Rows.Count, 1).End(xlUp).Row
    
    Total = Total + Cells(Coin, 1)
    If Total > Demand Then
        Cells(Coin, 1).Interior.ColorIndex = 6
        Total = 0
    Else
        Cells(Coin, 1).Interior.ColorIndex = xlNone
    End If
Next Coin

End Sub

If you want it to run only when you request it then just change the Sub from

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
to
Code:
Sub Test()
 
Upvote 0
I am sure there are other ways to accomplish this, but I am still learning the more in depth areas of VBA, but you can do the following and it should work (I also added this to the "Worksheet_SelectionChange" so anytime someone changes something it runs automatically").

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim inTotal As Integer, inCoin As Integer
Dim Coin As Long, Total As Long, Demand As Long
Dim TotallastRow As Long, CoinlastRow As Long

TotallastRow = Range("B" & Rows.Count).End(xlUp).Row
CoinlastRow = Range("A" & Rows.Count).End(xlUp).Row

inTotal = Cells(TotallastRow, 2).Value
inCoin = Cells(CoinlastRow, 1).Value

If TotallastRow < CoinlastRow Then
    Cells(TotallastRow + 1, 2) = inCoin + inTotal
End If

Demand = Cells(2, 3)


Total = 0


For Coin = 2 To Cells(Rows.Count, 1).End(xlUp).Row
    
    Total = Total + Cells(Coin, 1)
    If Total > Demand Then
        Cells(Coin, 1).Interior.ColorIndex = 6
        Total = 0
    Else
        Cells(Coin, 1).Interior.ColorIndex = xlNone
    End If
Next Coin

End Sub

If you want it to run only when you request it then just change the Sub from

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
to
Code:
Sub Test()

Thank you so much for your response dchaney!

I'm sorry, it seems to be just just adding 1 from the previous value.

Also I forgot to mention. What if B2 have already been set to a specific value, so I only want it to start adding B2+A3 and so on?
 
Upvote 0

Forum statistics

Threads
1,215,350
Messages
6,124,431
Members
449,158
Latest member
burk0007

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