How to Store Passwords To Multiple Sheets and To Retain thatPasswords When the Workbook IS closed

V

vicky

i need a code to store password to multiple sheets and to retain that
passwords when i closed the workbook. according to my code it can
assigns a password but cannot retain the multiple passwords assigned
to multiple sheet .

Sub ProtectAll()

Pwd = InputBox("Enter your password to protect all worksheets")
If Pwd <> "" Then
FormobjWSht_Input.Protect Password:=Pwd
Else
Exit Sub
End If

End Sub

Sub UnProtectAll()
'Dim Pwd As String

Pwd = InputBox("Enter your password to unprotect all worksheets",
"Password Input")
If Pwd <> "" Then
On Error Resume Next
FormobjWSht_Input.Unprotect Password:=Pwd
If Err <> 0 Then
MsgBox "You have entered an incorrect password. Worksheets could not "
& _
"be unprotected.", vbCritical, "Incorrect Password"
End If
 
J

john

Vicky,
see if this does what you want.

Sub ProtectAll()
Dim sh As Worksheet
Dim Pwd As String

Pwd = InputBox("Enter your password to protect all worksheets")
If Pwd <> "" Then

For Each sh In ActiveWorkbook.Worksheets

sh.Protect Password:=Pwd

Next sh

Else

Exit Sub

End If

End Sub

Sub UnProtectAll()
Dim sh As Worksheet
Dim Pwd As String

Pwd = InputBox("Enter your password to unprotect all worksheets",
"Password Input")

If Pwd <> "" Then

On Error GoTo myerror

For Each sh In ActiveWorkbook.Worksheets

sh.Unprotect Password:=Pwd

Next sh

myerror:
If Err > 0 Then

MsgBox "You have entered an incorrect password. Worksheets could
not " _
& "be unprotected.", vbCritical, "Incorrect Password"

Err.Clear

End If

End If

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