Workbook_BeforeClose (Cancel As Boolean)

rob_sheeds

Board Regular
Joined
Dec 9, 2010
Messages
57
Hi, I have read all there is in here on the Workbook_BeforeClose and I cant get it to work. The jist is to protect all sheets and the workbook whenever a user closes the workbook.
Current code is (which is under ThisWorkbook). Any help would be excellent and thanks in advance.

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

Call ProtectAllSheets

End Sub

Sub ProtectAllSheets()
'Protects ALL worksheets
For Each Shts In ThisWorkbook.Worksheets
Shts.Protect Password:="evo!@#"
Next
ActiveWorkbook.Protect Password:="evo!@#"
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Works OK for me....is there an error or is it just not protecting / closing ??
 
Upvote 0
HI Michael,
There is no error, it closes fine. I know something is happening as I save, a dialog appears, then I click the red X to close and am asked to save again, so something has changed and it is asking me to save the changes before close. But upon open the workbook is not protected. The macro to protect works fine too. Im stumped.
 
Upvote 0
in the private workbook_beforeclose subroutine, put the following lines, in that order.

Call ProtectAllSheets
ThisWorkbook.Close SaveChanges:=True
 
Upvote 0
it is just the normal SaveAs dialog box. I tried that code and nothing. Still doesnt protect anything.
Any way of working out if it is getting used?
 
Upvote 0
works fine for me. when I put the call protectallsheets after the other command. workbook saves and closes but no protection. however, in the above order, workbook protect,saves, and closes just fine. no dialog box.

BUUUTTTTT...

if you are getting a dialog box asking you if you want to save.. that would mean that the code Thisworkbook.close save changes:=true is not working..

hmm hmm.. did you by chance have designer mode enabled when you close the workbook? if you do, turn off designer mode, then give it a try again
 
Last edited:
Upvote 0
Ok, dumb question, but is this a new workbook...ie, Book1...that hasn't been saved before ??
 
Upvote 0
ok im confused...tried both having the SaveAs line before and after calling the other sub but doesnt work.
This is what I have. It is the 3rd module, and is in ThisWorkbook. My personal.xlsb is open too. Read somewhere that may be an issue? Im not familiar with debugging or break points, maybe that would help to see if it actually gets called?
Here is the code as it stands;
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)


ThisWorkbook.Close SaveChanges:=True
Call ProtectAllSheets


End Sub



Sub ProtectAllSheets()
'Protects ALL worksheets
For Each Shts In ThisWorkbook.Worksheets
Shts.Protect Password:="evo!@#"
Next
ActiveWorkbook.Protect Password:="evo!@#"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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