VBA Worksheet change event bypass?
This is an important modification to the answer from @Dick Kusleika.
When turning off the EnableEvents setting, it's a good idea to include error handling to turn EnableEvents back on. If you don't and the script throws an error, the change event trigger (your script) will stop working until you manually turn EnableEvents back on.
Ideally, you would place the following line immediately before any code that re-triggered your change event. The first line tells VBA to goto a label called "enableEventsOn" when it encounters an error. The second line bypasses change events.
On Error Goto enableEventsOn:
Application.EnableEvents = False
Then place this code immediately after code that re-triggered your change event. This turns change event triggers back on and returns normal error handling to your script.
Application.EnableEvents = True
On Error Goto 0
Finally, place this code at the end of your script. It's a label referred to above. If an error is encountered between "On Error Goto enableEventsOn:" and "On Error Goto 0" then the script will go here and turn EnableEvents back on, leaving it ready to start the script the next time you change your worksheet.
EnableEventsOn:
Application.EnableEvents = True
I think you want the EnableEvents
property of the Application
object. When you set EnableEvents
to False, then nothing your code does will trigger any events and none of the other event code will run. If, for example, your code changes a cell it would normally trigger the Change event or the SheetChange event. However, if you structure it like this
Application.EnableEvents = False
Sheet1.Range("A1").Value = "new"
Application.EnableEvents = True
then changing A1 won't trigger any events.
Sometimes it's beneficial to have your code trigger event code and sometimes it's not. Use EnableEvents
when you want to prevent it.