0

I created a simple value sum example driven by button click event as below.

Sub buttonEvent()

  Dim a As Double
  a = Application.WorksheetFunction.Sum(Range("A1:A10"))
  Cells(11, 1).Value2 = a

End Sub

GUI is like below.

enter image description here

When I click sum button, this macro sums values in defined range.

I would like to change the click event as range value change event.

It means, as soon as I change a value in the range, value sum function will be performed.

How can I do that?

1 Answer 1

1

Use these codes.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim a As Double
    If Not Application.Intersect(Target, Range("A1:A10")) Is Nothing Then
         a = Application.WorksheetFunction.Sum(Range("A1:A10"))
             Cells(11, 1).Value2 = a
    End If
End Sub

Edited after comment
For your clarification: enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

I have a question. Must I change the macro name as Worksheet_Change?
No. You have to use code in Worksheet_Change event. It is not a macro name. It is built in sheet event.
I am afraid I am a beginner of VBA. How could I make the Worksheet_Change event?
See the screenshot. I hope it will clear answer now to you. Let me know if you still need clarification.
Double click on Sheet1 in left side pane. Select Worksheet from above dropdown. Then select Change from right dropdown. Now copy paste above codes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.