Workbook_BeforeClose Event

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,913
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
If I understand this article correctly:

Rich (BB code):

it claims the Workbook_BeforeClose event does NOT necessarily kick off code saved there.

Therefore this workaround has been suggested:

Rich (BB code):
Its true that if close Excel manually the IgnoreRemoteRequests setting reverts to what iwas on opening - but NOT if you close Excel programmatically (via Application.Quit).

 The generic solution to the problem (of Workbook_BeforeClose ignoring some VBA commands if triggered by ActiveWindow.Close) is this:

 a. write your own module-level procedure, eg My_WB_BeforeClose_Stuff()
 b. declare a public boolean Excel_Exited_Manually and set its intial value to True
 c. create a workbook-level event WorkBook_BeforeClose containing only

 If Excel_Exited_Manually then My_WB_BeforeClose_Stuff

 d. in your code wherever you use ActiveWindow.Close (or ThisWorkbook.Close) precede it with

 My_WB_BeforeClose_Stuff
 Excel_Exited_Manually = False

 Then your single My_WB_BeforeCloseStuff code will work when the spreadsheet is closed manually or programmatically (eg via your own Exit button on a form or command-bar).

My question is why not simply do this?

Rich (BB code):
Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Call My_WB_BeforeClose_Stuff

End Sub

Sub My_WB_BeforeClose_Stuff()
    
    Application.IgnoreRemoteRequests = False
    
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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