• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Workbook before close

Costas

Member
Hi Chandoo,

I've written some code for the before close event so that I unprotect my workbook, hide all sheets but a blank one and password protect my workbook again. All this is working fine but I keep getting asked if I want to save changes after I run all the code which means that the user will be able to see the changes I've done. Is it possible to avoid getting asked to save?
Private Sub Workbook_BeforeClose(Cancel As Boolean)

If MsgBox("Are you sure that you want to close this workbook ?", 36, "Confirm") = vbNo Then
Cancel = True
Else:
ActiveWorkbook.Save
Cancel = False
Application.ScreenUpdating = False
UnprotectWorkbook
Sheets("Sheet1").Visible = True
For Each MySheet In MyBook.Worksheets
If MySheet.Name = "Sheet1" Then
MySheet.Visible = True
Else
MySheet.Visible = False
End If
Next MySheet
ProtectWorkbook
End If

End Sub

Thanks
Costas
 
Hi Nebu,

I tried that but it doesn't work because it restarts the before close event and I end up in a loop.
 
as i close the workbook my all sheets should be protected with password. please help and provide the coding.
Hi Ram,

Pls always start a new thread for very own question.

Check this..

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet, myP As String

myP = "passTolock"

Application.ScreenUpdating = False

For Each ws In ThisWorkbook.Worksheets
    ws.Protect myP
Next

ThisWorkbook.Close True

Application.ScreenUpdating = True

End Sub
 
Hi Ram,

Pls always start a new thread for very own question.

Check this..

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet, myP As String
 
myP = "passTolock"
 
Application.ScreenUpdating = False
 
For Each ws In ThisWorkbook.Worksheets
    ws.Protect myP
Next
 
ThisWorkbook.Close True
 
Application.ScreenUpdating = True
 
End Sub
thanks friend for the solution. workseets are not being protected when i close the file. please provide solution
 
Back
Top