The Workbook_BeforeClose event in not working

sbuffington

New Member
Joined
Jan 14, 2019
Messages
8
I created the popup calendar from Martin Green's site, saved it in a personal project like it said, and I keep getting the "Insert Date" adding to the context menuwhen you right click in a cell tofirethe popup calendar. The only thing that I can imagine is that the Workbook_BeforeClose code is not working. I've tried including the Application.EnableEvent=true in immediate window and that doesnt seem to work either.
Code:
Private Sub Workbook_Open()
    Dim NewControl As CommandBarControl
    Application.OnKey "+^{C}", "Module1.OpenCalendar"
    Set NewControl = Application.CommandBars("Cell").Controls.Add
    With NewControl
        .Caption = "Insert Date"
        .OnAction = "Module1.OpenCalendar"
        .BeginGroup = True
    End With
    Application.EnableEvents = True
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    On Error Resume Next
    Application.CommandBars("Cell").Controls("Insert Date").Delete
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Testing the Workbook_BeforeClose event is easy enough. Adding a msgbox statement as the second line should tell you if the event is firing.

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    MsgBox "This is the Workbook_BeforeClose event"

    On Error Resume Next
    Application.CommandBars("Cell").Controls("Insert Date").Delete
End Sub

Also a subroutine like this that you run manually from the debug window:

Code:
Sub EventsOn()
    Application.EnableEvents = True
    MsgBox "Events Status: " & Application.EnableEvents
End Sub

can be useful for those times when you are unsure if events are enabled.
 
Upvote 0
Create a new workbook and add this code to the workbook code module

Code:
Private Sub Workbook_Open()
 MsgBox "This is the Workbook_Open event", vbInformation, ThisWorkbook.Name
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    MsgBox "This is the Workbook_BeforeSave event", vbInformation, ThisWorkbook.Name
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    MsgBox "This is the Workbook_BeforeClose event", vbInformation, ThisWorkbook.Name
End Sub

Then save, close, and reopen the new workbook to test event functionality. If the events fire in your new workbook then they should fire in your current workbook, and you should inspect your code more closely to determine where you might be turning off events.
 
Upvote 0
Thank you for the response...the code you supplied works as expected. Could the issue be that my code is in a personal.xlsb workbook. Basiclly the only thing the sersonal does is allow me to use a pop up calendar by right in a cell, the workbook close event is to remove the insert date context menu item
 
Upvote 0
Could the issue be that my code is in a personal.xlsb workbook.
Yes; that event will fire when Personal.xlsb is closed.
 
Upvote 0
It only fires when the workbook it's in closes; that's Personal.xlsb.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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