Difference Between Workbook_Deactivate() & Before Close

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Difference Between Workbook_Deactivate() & Before Close

Post by MSingh »

Hi,

Could you please explain what is the difference between:

Private Sub Workbook_Deactivate

And

Private Sub Workbook_BeforeClose

Thanks
Mohamed

User avatar
HansV
Administrator
Posts: 79030
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Difference Between Workbook_Deactivate() & Before Close

Post by HansV »

Workbook_Deactivate occurs when you switch away (within the Excel application) from the workbook that contains the code, for example when you open another workbook, or when you activate another, already opened, workbook, and also when you close the workbook.

You can use this event for example to disable features that you don't want to be available when the workbook containing the code is not active, for example a custom toolbar button or a custom keyboard shortcut. You'd usually have code in the Workbook_Activate event to enable them again when the user switches back to the workbook.

Workbook_BeforeClose occurs when the user starts to close the workbook, but before the workbook has actually been closed, for example because the user clicks on the close button (the "X") in the upper right corner, or because the user presses Ctrl+F4.

This event has a Cancel argument that is False by default. If you set it to True within the event procedure, you cancel the closing of the workbook, i.e. it remains open.

You can use this event procedure to check whether it's OK to close the workbook (and cancel if not), and also to remove features that shouldn't remain available after the workbook has been closed.
Best wishes,
Hans

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Re: Difference Between Workbook_Deactivate() & Before Close

Post by MSingh »

Thanks Hans,

Your understand my project very well!

Kindest Regards
Mohamed

matonanjin
NewLounger
Posts: 4
Joined: 01 Aug 2023, 21:54

Re: Difference Between Workbook_Deactivate() & Before Close

Post by matonanjin »

Thanks Mohammed @MSingh It has been 13 years and 8 days. So I don't know if you are still around. But I wanted to know this. I still don't understand it but I will study it. Thanks for asking what I want to know

User avatar
HansV
Administrator
Posts: 79030
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Difference Between Workbook_Deactivate() & Before Close

Post by HansV »

Welcome to Eileen's Lounge!

Mohamed Singh hasn't been online here for quite a while, but I'm glad to hear that his question was useful to you.
Best wishes,
Hans