Macro to unprotect with password

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
 
T

Tieske

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top