Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Worksheet change event bypass?

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.

like image 308
user1545643 Avatar asked Apr 02 '13 10:04

user1545643


People also ask

How do you skip worksheets to change events in VBA?

In most VBA code, you can use the Application. EnableEvents property to prevent Excel from calling event procedures. This often used in the Change event of a worksheet if the Change event itself changes a cell value.

What triggers worksheet change event?

Workbook SelectionChange Event The selection change event is triggered whenever there is a selection change in the worksheet. The below code would recalculate the sheet as soon as you change the selection. Another example of this event is when you want to highlight the active row and column of the selected cell.

Can you have 2 worksheet change events?

You cannot have more than one Worksheet_Change event procedure in the same worksheet module.

When can worksheet change event be triggered?

Occurs when cells on the worksheet are changed by the user or by an external link.


2 Answers

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.

like image 147
Dick Kusleika Avatar answered Sep 22 '22 08:09

Dick Kusleika


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
like image 35
ChrisB Avatar answered Sep 20 '22 08:09

ChrisB