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
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
18,185
Office Version
2013
Platform
Windows
Works OK for me....is there an error or is it just not protecting / closing ??
 

rob_sheeds

Board Regular
Joined
Dec 9, 2010
Messages
57
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.
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
18,185
Office Version
2013
Platform
Windows
What's in the Dialog Box ??
 

ttdk1

Board Regular
Joined
May 21, 2014
Messages
189
in the private workbook_beforeclose subroutine, put the following lines, in that order.

Call ProtectAllSheets
ThisWorkbook.Close SaveChanges:=True
 

rob_sheeds

Board Regular
Joined
Dec 9, 2010
Messages
57
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?
 

ttdk1

Board Regular
Joined
May 21, 2014
Messages
189
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:

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
18,185
Office Version
2013
Platform
Windows
Ok, dumb question, but is this a new workbook...ie, Book1...that hasn't been saved before ??
 

rob_sheeds

Board Regular
Joined
Dec 9, 2010
Messages
57
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
 

Forum statistics

Threads
1,082,102
Messages
5,363,137
Members
400,720
Latest member
Pettel

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top