2

I have workbook (1) with many codes depend on events to be enabled to run, by chance I opened workbook (2) and disabled events in it but I surprised that events also disabled in Workbook (1) . So , I tried to to put this code in Workbook (1) Application.enableEvents= True in Workbook_Activate but does not work even Window_Activate and Worksheet_Activate . Any help please

Leedo
  • 439
  • 5
  • 18

1 Answers1

3

Let's say you live in an apartment which has 3 rooms. When you do not need lights in one room, you do not switch off power of the entire apartment and then wonder why is there no light in the 2nd or 3rd room? For this purpose you have switches in those rooms so that you can switch off light only for the relevant room.

Once you undertsand the above logic, then try and understand this.

Let's say your Excel Application (Apartment) has 3 workbooks (Rooms). If you switch off the event for the application and not for the (procedures/functions) in that workbook, then how will it function for other workbooks (Room)?

It is advisable to switch off events inside the function/procedure and turn them back on in the end (just like using ON/Off switches). Here is an example

Private Sub Sample()
    Application.EnableEvents = False
    
    '
    '~~> Your code here
    '
    
    Application.EnableEvents = True
End Sub

Also whenever you are switching off events, use error handling to turn it back on, else if you get an error, the code will not run the next time. Here is an example

Private Sub Sample()
    On Error GoTo Whoa
    
    Application.EnableEvents = False
    
    '
    '~~> Your code here
    '
    
Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub

Note: Links worth reading Application.EnableEvents property (Excel) and Application object (Excel)

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • so it is a limitation in Excel itself , I asked this question because my workbook has code to log any change of data done by any users, this log code depends on events to work. So , any user can easily open other workbook and disable events in it then he can do any thing on the main workbook which has log codes. – Leedo Jun 29 '21 at 08:41
  • No it is not a limitation. Your code must be inside a procedure. simply switch off/on events in that procedure as I have shown above. I guess you are using `Worksheet_Change event`? If the user makes any changes in the relevant workbook, it will still work and when the editing stops, the code will switch off events and exit sub. Just like you going into room to turn on light and then turning them off on your way out. – Siddharth Rout Jun 29 '21 at 08:44
  • Do you advice to open a new question with my log code to solve this security violation @ – Leedo Jun 29 '21 at 08:57
  • Yes , the log code depends on Worksheet_Change event – Leedo Jun 29 '21 at 08:59
  • Then I would recommned seeing [Working with Worksheet_Change Event](https://stackoverflow.com/questions/13860894/why-ms-excel-crashes-and-closes-during-worksheet-change-sub-procedure/13861640#13861640) – Siddharth Rout Jun 29 '21 at 09:02