Macro to unprotect with password

  • Thread starter Thread starter Brian.Hastings82
  • Start date Start date
B

Brian.Hastings82

Hi Excel People

I need a macro that when pressed asks for the passwork to unprotect a
cell/worsheet.

If possible i would then like another input box asking for a number
which it inserts into this cell then locks again.

any ideas?

Thanks

Brian
 
Create a cell with the password you need and name it "GetThePassword". Then
try the code below. You should be able to adapt it to suit your needs.

'Make workbook ready for development
'
Sub StartDevelopment()

Dim mySheet As Worksheet
Dim Password As String

' read the password from the named range
Password = [GetThePassword]

'check password
If UCase(InputBox("Password: ")) <> UCase(Password) Then
MsgBox "Wrong password.", vbOKOnly + vbCritical, "Sorry..."
Exit Sub
End If

'unprotect the workbook, and then all the sheets
ActiveWorkbook.Unprotect (Password)
For Each mySheet In Sheets
With mySheet
.Unprotect (Password) 'unprotect the sheet
.Visible = xlSheetVisible 'unhide the sheet
End With
Next

End Sub
 
Back
Top