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
 
in the private workbook_beforeclose subroutine, put the following lines, in that order.

Call ProtectAllSheets
ThisWorkbook.Close SaveChanges:=True

need to be in that order. Also make sure designer mode is disabled. I tested it with designer mode on and got a dialog box when closing, so that might be an issue.
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Nerrr the code was not in ThisWorkbook.... my bad guys. Im an accountant not a guru like you all.
Appreciate your help as always!!!!!!!
 
Upvote 0
Hmm....Seniors moment ???...(y)....I have 'em all the time !!!
 
Upvote 0

Forum statistics

Threads
1,216,146
Messages
6,129,134
Members
449,488
Latest member
qh017

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