I have encountered a strange event behavior, which I do not understand. Please help. Following is an explanation of the problem.
- In my Worksheet_Change event, at the beggining I have set Application.events = False.
- Then I run main code that changes few things including formulas.
- After the main code is done I reset the Application.EnableEvents = True.
- Then the Worksheet_Change event ends at Exit Sub.
- Right after the code finishes it triggers Worksheet_Calculate event, even when there is no code after Application.EnableEvents = True. I do not want this event to be triggered.
Here is a snapshot of a code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Main code
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub
Private Sub Worksheet_Calculate() 'triggered without any reason?
... 'code here
exit sub
So the question is : How is it possible that after the code ends, it immediately triggers the Worksheet_Calculate event? There is nothing after the Application.EnableEvents = True to trigger this event.
Thank you for help. I don't understand this one.
Edit and possible explanation
I've been thinking and testing. There may be nothing wrong with that behavior.
Both events are triggered at the same time and it has to pick one code to run before another. The main code
inside Private Sub Worksheet_Change(ByVal Target As Range)
is NOT triggering the Private Sub Worksheet_Calculate()
. The latter is in queue, waiting for it's turn.