I am fixing a spreadsheet. The programmer made a macro for each sheet to fire when the sheet is changed. This is good because it colour co-ordinates the sheet details when new information is added so I would like to keep this feature.
I have written a macro which sorts the data and allows for removal and addition of new employees, this is in conflict with the change event macro and is causing my macro to have errors if they are both operational.
Q. Is there a way to bypass the worksheet change event while the macro is running and then have it in place again once the macro is finished?
Here is the code for the change event.
Private Sub Worksheet_Change(ByVal target As Excel.Range, skip_update As Boolean)
If skip_update = False Then
Call PaintCell(target)
End If
End Sub
My macro is bringing up errors when I refer to worksheets or ranges.
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 to have your code trigger event code and sometimes it's not. Use EnableEvents
when you want to prevent it.
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
Too risky in my own opinion.
I'd use a variable, named "skip_update" that is set to "True" in the beginning of each macro and to false when it ends.
e.g :
dim skip_update as Boolean = False
sub auto_macro_when_modify()
if skip_update == True then
exit sub ' NO CHANGE !
end if
end sub
sub other_macro
skip_update = True
' ( do stuff)
skip_update = False
end sub
You can hold down the CTRL key while doing an entry that would trigger the change on event routine. I have a routine to sum the values in a selected range in a given column. If I drag cursor from rows 1 to 5, highlighting them, then the sum of these is saved to the clipboard. Any entry in a cell in this column triggers the routine. If I hold down the CTRL key while doing the intended action or any entry in a cell in the column the routine is not initiated. May not work for more complicated ChangeEvent scenarios.
来源:https://stackoverflow.com/questions/15761530/vba-worksheet-change-event-bypass