Can't step into Workbook_BeforeClose event

shawntanner

New Member
Joined
May 4, 2006
Messages
23
I don't know what I did, but somehow my Workbook_BeforeClose event will not work. I can't even step into it (F8). And, with my cursor in the event, when I press F5 (Run Sub/Userform), the Macros dialog box shows up (as if I'm not within an event). It only occurs in the BeforeClose event.

I even tried making a Workbook_BeforeClose event in a new workbook and just put a MsgBox "Will this work?" in it to test. F8 (step into) will not work (I just get the ding sound), but when I close the new workbook with the X button, the debugger will go to the break point I set on Private Sub Workbook_BeforeClose(Cancel As Boolean).

Any advice is greatly appreciated. Please let me know if I need to clarify anything.

Thanks in advance!
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I can't even step into it (F8). And, with my cursor in the event, when I press F5 (Run Sub/Userform), the Macros dialog box shows up (as if I'm not within an event).
I believe that is normal behaviour for 'event' code.

Your original problem:
It is possible that you code is not working because 'Events' are disabled. In the VB window, ensure the Immediate Window is visible (View|Immediate Window) and then on a new line in the Immediate Window, type
Application.EnableEvents=True and press Enter

Put a Breakpoint or message box in your workbook_close code.
Now go back to your workbook and try closing. See if the code is triggered now.
 
Upvote 0
You cannot step into or directly run any Sub or Function that carries arguments .
 
Upvote 0
Workbook_BeforeClose event cannot be stepped into. Here is what you need to do to make it work.

1) Open Excel - Type some data. Save the file.
2) Goto to VB Editor - ThisWorkbook - Goto Workbook_BeforeClose.
3) Type msgbox "This will Work"
4) Save the file.
5) Try closing excel file. This will show up the msgbox which you need.

The event will occur only before the file is closed. You cant step into it. You can use break point to navigate through the code in Workbook_BeforeClose event.
 
Upvote 0
Jaafar

Are you totally sure about that?

I can set a breakpoint in the BeforeClose event and it works.:)

I can also set a breakpoint in the 2nd sub here and it works.
 
Upvote 0
Peter (and everyone else that replied so quickly!)
Thank you! That worked! (Sorry for the delay - I couldn't get back here until today!)

I did try the Application.EnableEvents=True within the Workbook_Open event last Thursday but apparently I must be turning off events somewhere else within my code. I didn't know that you could run code from the immediate window. Looks like I've got more research to do!

Thanks again - this was killing me last Thursday!
 
Upvote 0
FYI - It only took a moment to follow my code and see where I was exiting a sub and leaving Application.ScreenUpdating, Application.DisplayAlerts, & Application.EnableEvents as False! Fixed it by using goto instead of Exit Sub.

Thanks again everyone!
 
Upvote 0

Forum statistics

Threads
1,213,557
Messages
6,114,293
Members
448,564
Latest member
ED38

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top