Why Application.EnableEvents = False and =True in an event macro

still learning

Well-known Member
Joined
Jan 15, 2010
Messages
784
Office Version
  1. 365
Platform
  1. Windows
Hi,

Way back in 2020 I got this from this site (Thank you) after asking how to have an event macro change a cell to proper case after typing in a text.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 3 Then
Application.EnableEvents = False
Target.Value = StrConv(Target.Value, vbProperCase)
Application.EnableEvents = True
End If
End Sub
I’m finally getting around to asking about it
Why do I need

Application.EnableEvents = False
and
Application.EnableEvents = True
AND
Why doing the macro without them closes the program?



mike
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Because it's a Change event.....everytime something gets changed, the code runs again, which makes the change again and it runs again....ad infinitum
By using those 2 lines it stops any change events until the changes have been done then reenables and exits the macro
Have a read here...
 
Upvote 0
Solution
Thanks Michael M
Very informative
I just checked and you were one of the two members that gave me this code

thanks again

mike
 
Upvote 0
Well there you go....and I'm still here...:cool:
 
Upvote 0
Hi Michael, are you still there ?

I read that the 'false' line needs to be inserted immediately after the 'dim' statement and the 'true' line inserted immediately before the 'End If'.

Can you just clarify if a false/true line is needed for each one of those within the change event or is it sufficient for the 'false' line just be inserted at the beginning of the first sub and before the end of the last one?

Many thanks!
 
Upvote 0
It needs to be inserted after the action has been completed.
If you put it at the end of the code you may get Incorrect results if other actions need to be carried out relative to a previous action
 
Upvote 0
Sorry Michael, which action? Would you mind explaining it in simple terms I can understand - once or 6 times please?

Thanks again!
 
Upvote 0
Sorry Michael, which action? Would you mind explaining it in simple terms I can understand - once or 6 times please?
I'm afraid Michael won't really be able to answer that as it depends on exactly what the various parts of your code are doing.
If you need other events to trigger then you would need to do it multiple times, if you only have the one event then only the once.
Can you just clarify if a false/true line is needed for each one of those within the change event or is it sufficient for the 'false' line just be inserted at the beginning of the first sub and before the end of the last one?
Your question here is a bit confusing , at the start you ask about within the change event and at the end about multiple subs.

In general if it is within the same event then you only need to set it / unset it once, but as I stated above it depends on exactly what you are doing in your code as there are occasions when you need to set it / unset it multiple times

If dealing with multiple subs then technically you could set it to false in the first sub and true in the last sub if you don't need any event codes running in between BUT it is a risky way of doing it, stick to setting it back to true as soon as each event has finished it's task.
 
Upvote 0
I'm afraid Michael won't really be able to answer that as it depends on exactly what the various parts of your code are doing.
If you need other events to trigger then you would need to do it multiple times, if you only have the one event then only the once.

Your question here is a bit confusing , at the start you ask about within the change event and at the end about multiple subs.

In general if it is within the same event then you only need to set it / unset it once, but as I stated above it depends on exactly what you are doing in your code as there are occasions when you need to set it / unset it multiple times

If dealing with multiple subs then technically you could set it to false in the first sub and true in the last sub if you don't need any event codes running in between BUT it is a risky way of doing it, stick to setting it back to true as soon as each event has finished it's task.
Many thanks Mark. I'll post back with the complete event in due course.
 
Upvote 0
stick to setting it back to true as soon as each event has finished it's task.
Which can be equally risky if it is set back to true in one procedure / event and control returns to another that has not yet finished and still requires it to be set to false.

Personal preference in scenarios where multiple events / procedures are triggering each other is to check status of events and calculation as the first step, set them to false and manual respectively, then return them to their original status at the end rather than risk setting them back to true and auto too soon.
 
Upvote 0

Forum statistics

Threads
1,215,335
Messages
6,124,327
Members
449,155
Latest member
ravioli44

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