PDA

View Full Version : [SOLVED:] Lock sheet-macro



LordRaper
11-29-2014, 04:47 AM
How to lock sheet when i wright in cell "a1" password to lock sheet with macro ...
sheet must be lock except cell "a1" for pasword to unlock sheet
ty...

sorry for bad english

westconn1
11-29-2014, 01:50 PM
see if this works for you


Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("a1")) Is Nothing Then
If Target.Value = "mypass" Then Target.Worksheet.Unprotect "mypass"
End If
End Sub

Sub locksheet()
Dim s As Worksheet
Set s = Sheets("sheet1")
s.Cells.Locked = True
s.Range("a1").Locked = False
s.Protect "mypass"
End Sub
you can store the password elsewhere and use a variable in the code

LordRaper
11-29-2014, 02:29 PM
its good but i must run macro after i wrote password
but i need another solutions when i wrote password automatic look sheet not after i run macro
ty for help

westconn1
11-29-2014, 07:09 PM
ok, try like

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("a1")) Is Nothing Then
If Target.Value = "mypass" Then
With Target.Worksheet
If .ProtectContents Then
Target.Worksheet.Unprotect "mypass"
Else
.Cells.Locked = True
.Range("a1").Locked = False
.Protect "mypass"
End If
.Range("a1").Value = ""
End With
End If
End If
End Sub

LordRaper
11-30-2014, 03:59 AM
works great ...

ty for all