Change event triggered after macro is finished

bergie7isu

New Member
Joined
Dec 1, 2010
Messages
14
Long time browser, first time poster.

I've got a problem with a worksheet change event being triggered after I manually run a macro. The issue is that the change event actually happens before the manually run macro is even started. I've duplicated the issue in a much simpler example. Code below.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A3")) Is Nothing Then
    Range("A5").ClearContents
End If
End Sub

Sub calc()
Range("A5") = Range("A1") + Range("A2") + Range("A3")
End Sub

So, in this simplified example, I've got a "Calc" button that runs a macro to sum the values of three cells (A1, A2, and A3) and output the result to A5. My worksheet change event handler clears the value out of A5 if any of the values in the input cells are changed. Everything works fine until I change the value of one of the input cells but don't hit enter or click into a different cell before clicking the "Calc" button. In that scenario, the macro recognizes the new value I put into the cell (and calculates the correct sum...which I know by throwing a quick msgbox into the calc macro), but the result doesn't show in A5 because it's cleared by the change event handler. It's like the change event that was started before I ran the macro doesn't actually complete until after the macro runs...except the macro does recognize that the input values changed.

Anybody know how I can get around this?

Thanks!
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Long time browser, first time poster.
Welcome back (looks like you did make some posts way back in 2010!) ;)

Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If ActiveCell.Address(0, 0) = "A5" Then Exit Sub

    If Not Intersect(Target, Range("A1:A3")) Is Nothing Then
        Application.EnableEvents = False
        Range("A5").ClearContents
        Application.EnableEvents = True
    End If
    
End Sub


Sub calc()
    Range("A5").Activate
    Range("A5") = Range("A1") + Range("A2") + Range("A3")
End Sub
 
Upvote 0
Welcome back (looks like you did make some posts way back in 2010!) ;)

Well, look at that...right you are! I guess I've slept a couple of times since then.


I'm still getting the same issue. It's not the writing to A5 that's triggering the change event...as evidenced by the fact that the value isn't cleared if I've clicked out of the input cells prior to running the macro.

Also, when I throw in a msgbox to tell me what the code thinks A5 should be, it's correct, and I can see the value populate into A5 while the msgbox is displayed. Then it clears afterward...which HAS to be from the previously initiated change event on the input cells, right?

Code:
Sub calc()
Range("A5") = Range("A1") + Range("A2") + Range("A3")
MsgBox Range("A5").Value
End Sub
 
Upvote 0
Yes, that line is important to the code.

You are welcome.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

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