• 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 Protection Question

have several tabs in my workbok with macros in the file. I want to protect my worksheets so users can't change them only use the functions on it. I found this code which protected all my worksheets and worked great. The sheets where protected and my macro's ran in the background without any unprotect code in all the macro's.


However that was only for Protect Worksheet. I want to Protect Workbook so people can't unhide tabs on their own or stuff like that. When I did that, the macro's didn't run anymore. Does anyone know any way to Protect Workbook and still have all the macro's run, or something to add to this code.


Private Sub Workbook_Open()


Dim Sh As Worksheet


For Each Sh In Worksheets

Sh.Protect UserInterFaceOnly:=True

Next


End Sub
 
[pre]
Code:
Private Sub Workbook_Open()

Dim Sh As Worksheet
ThisWorkbook.Unprotect
For Each Sh In ThisWorkbook.Worksheets
Sh.Protect UserInterFaceOnly:=True
Next
ThisWorkbook.Protect
End Sub
[/pre]
 
That didn't work. I put that sub into my worksheet.


I clicked a button to see a view and their first line in the macro linked to that button is

Sheets("Sheet2").Visible = True

The debugger stopped it on that line.
 
Any macro that you want to run will need to unprotect the applicable object, be that the workbook and/or the worksheet. E.g.,


Sub SomeOtherMacro()

ThisWorkbook.Unprotect

'Do some other stuff

'Unhide some sheets

'Hide the sheets

'etc.

ThisWorkbook.Protect

End Sub
 
Back
Top