Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing resultant cell value from an addition and then adding it to new results every time

Tags:

excel

vba

I am creating a model in Excel in which a cell computes the sum of two cells and keeps adding to the value every time the two cells on which it is dependent change values. For example,

If,

A + B = C

Then,

Instance 1: -> 5 + 5 = 10

Instance 2: -> 4 + 3 = 17

Instance 3: -> 2 + 3 = 22

and so on...

I believe this can be achieved through a VB script.

like image 432
Sharat Prakash Avatar asked Jan 28 '26 23:01

Sharat Prakash


1 Answers

you only need to put the following code in the worksheet code pane:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Range("A1:A2"), Target) Is Nothing Then Exit Sub
    Range("B1") = Range("B1") + WorksheetFunction.sum(Range("A1:A2"))
End Sub

of course you have to change:

  • all "A1:A2" occurrences to your actual cells to sum address

  • all "B1" occurrences to your actual cell holding the running sum address

like image 153
DisplayName Avatar answered Jan 31 '26 13:01

DisplayName



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!