Log in

View Full Version : Workbook protected when using this code



raybugge
07-08-2013, 09:14 AM
Greetings

This code works great but can it be altered so that when the workbook is closed the Sheets are always protected.This way whenever the workbook is opened the sheets are in protected mode

Cheers




Private Sub CommandButton1_Click()

Dim strPassword As String
Const strActualPassword As String = "ABCD"
strPassword = InputBox("Please enter the password", "Protect/Unprotect Sheet")

If strActualPassword = strPassword Then
If Me.CommandButton1.Caption = "PROTECT SHEET" Then
Me.CommandButton1.Caption = "UNPROTECT SHEET"
UnlockCells
Me.Protect Password:=strPassword
Else
Me.CommandButton1.Caption = "PROTECT SHEET"
Me.Unprotect Password:=strPassword
End If
Else
MsgBox "Invalid Password"
End If

End Sub

Sub UnlockCells()

Me.Range("B10:T10,B16:T18,B24:T28").Locked = False

End Sub

Excel Fox
07-08-2013, 07:15 PM
Use this in the workbook module only


Private Sub Workbook_BeforeClose(Cancel As Boolean)

Dim wks As Worksheet

For Each wks In ThisWorkbook.Worksheets
wks.Protect "passwordhere"
Next wks
If Not Cancel Then
Me.Save
End If

End Sub