0
votes

Using Excel 2011 (should be same as Excel 2010)

Code is under the "ThisWorkbook" module in Excel

Events are enabled

Macros are enabled

I can't seem to get either Workbook_Open() or Workbook_BeforeClose() to execute. I've read numerous posts on the subject but no solution. Here is some simple test code that should execute but doesn't. Any help would be greatly appreciated.

Private Sub Workbook_Open()
    ActiveSheet.Range("BL4").Value = "Open is working"
End Sub


Private Sub Workbook_BeforeClose(Cancel As Boolean)
    On Error Resume Next 'in case the menu item has already been deleted
    ActiveSheet.Range("BL5").Value = "Close is working"
End Sub
1
Do you get an error when you comment out On Error Resume Next? Try adding MsgBox "Hello" to both events to see if at least a message will be displayed. - Barry
Did you mean to say 'ThisWorkBook' not 'ThisSheet'? - Richard Morgan
They can be private. It will make no difference. - JNevill
Did you put these in the Workbook object or in a separate module? - JNevill
Just for completeness, your code works fine for me on a Windows 7 PC running Excel 2013 and on my MacBook Pro also running Excel 2013 under OS X. Just a silly thought but which nearly threw me. You are looking at the right cells BL4 and 5 are off screen to the right? - barryleajo

1 Answers

1
votes

First make sure you have put this in this in the right place and have macros enabled.

Then, try adding this line to the workbook_open method:

MsgBox "HELLO"

Do you see the msg box? You're choice of cell looks a bit strange

Also, I think you need to use a .xlsm file not .xlsx (Although not sure on that one)

FInally, if a plugin calls something like this line, it could cause your events not to fire..

Application.EnableEvents = False

So make sure you have tested it with no other sheets or addins open.