Wardylewis
New Member
- Joined
- Jun 7, 2016
- Messages
- 37
Good Morning,
I have put a lock button in to my Excel workbook to lock all sheets when I press the button.
As a stupid person I keep forgetting sometimes to press it to lock the sheet so I am looking to automate it so that if it hasn't been pressed when before I close it then it locks itself as a fail safe.
Could you please provide any advice which would be greatly appreciated.
I have put a lock button in to my Excel workbook to lock all sheets when I press the button.
As a stupid person I keep forgetting sometimes to press it to lock the sheet so I am looking to automate it so that if it hasn't been pressed when before I close it then it locks itself as a fail safe.
Code:
Private Sub CommandButton1_Click()Dim ws As Worksheet
Dim pwd As String
pwd = "MY PASSWORD" ' Put your password here
For Each ws In Worksheets
ws.Protect Password:=pwd
ws.Protect AllowFiltering:=True
ws.Protect AllowUsingPivotTables:=True
Next ws
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Worksheets
If sht.Protection = False Then
sht.Protect ("MY PASSWORD", = True )
End If
Next sht
End Sub
Could you please provide any advice which would be greatly appreciated.