Add values to total values in cells

ruru

New Member
Joined
Aug 15, 2007
Messages
2
Hi, guys
I able to get the total value at B8, but when come to create the next column, D8 doesn't appear the total value.
Below are my code. Anyone able to help me? Thank in advance.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)

Static A, C As Double

If Target.Address = "$A$8" Then
A = Range("B8").Value
Range("B8").Value = A + Target.Value

If Target.Address = "$C$8" Then
C = Range("D8").Value
Range("D8").Value = C + Target.Value

End If
End If
End Sub
 

Some videos you may like

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
This works for me

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Address = "$A$8" Then
    Range("B8").Value = Range("B8").Value + Target.Value
ElseIf Target.Address = "$C$8" Then
    Range("D8").Value = Range("D8").Value + Target.Value
End If
End Sub
 

al_b_cnu

Well-known Member
Joined
Jul 18, 2003
Messages
4,494
Hi,

I think you've got your Ends If in the wrong place :¬/

Try this slightly more robust code:
Code:
Dim A As Double, C As Double

If Target.Address = "$A$8" Then
    A = Val(Range("B8").Value)
    Range("B8").Value = A + Val(Target.Value)
End If
If Target.Address = "$C$8" Then
    C = Val(Range("D8").Value)
    Range("D8").Value = C + Val(Target.Value)
End If
Note that in your posted code, variable A will be defined as a Variant type
 

ruru

New Member
Joined
Aug 15, 2007
Messages
2
Hi guys,
Its works now.
I'm new to VBA, still have alot to learn from u guys
Thanks.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,814
Messages
5,598,246
Members
414,219
Latest member
ruchperformive

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
Top