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
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
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
 
Upvote 0
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
 
Upvote 0
Hi guys,
Its works now.
I'm new to VBA, still have alot to learn from u guys
Thanks.
 
Upvote 0

Forum statistics

Threads
1,217,473
Messages
6,136,856
Members
450,027
Latest member
Apexwolf

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