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
 
Yes; that event will fire when Personal.xlsb is closed.

Not in my experience. I recall that there is an exception for that event for the personal macro workbook (but I cannot find the original reference). I just re-tested under both XL2010 and 2016. The Before_Close event in personal.xlsb did not fire when excel is closed.
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
The event will not fire from the personal workbook


Try using auto_close (note that this must go in a standard code module, and not in the workbook code module). That should work with personal.xlsb
Code:
Sub Auto_Close()
    Dim Msg As String

    Msg = "This is the Auto_Close subroutine for " & ThisWorkbook.Name & vbCr & vbCr
    Msg = Msg & "Workbook event status: " & Application.EnableEvents
    MsgBox Msg, vbInformation, ThisWorkbook.Name

    On Error Resume Next
    Application.CommandBars("Cell").Controls("Insert Date").Delete
End Sub
 
Upvote 0
If you want to keep the event in Personal, put this in Personal's ThisWorkbook module:

Code:
Option Explicit

Dim WithEvents app  As Excel.Application

Private Sub Workbook_Open()
  Set app = Excel.Application
End Sub

Private Sub app_WorkbookOpen(ByVal Wb As Workbook)
  ' do stuff here
End Sub

You'll need to close and restart Excel (or run Workbook_Open) to initialize.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,335
Members
452,636
Latest member
laura12345

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