A P
A P

Reputation: 35

A button to protect all worksheets with a password and a button to unprotect

I have a Workbook with around 180 Worksheets.

I am trying to create 2 macros:

1) to protect all worksheets upon clicking a button

2) to unprotect all worksheet upon clicking a button, but asks user for the password

Here what i have already...

Sub Protect()

Dim ws As Worksheet
Dim pwd As String

pwd = "xyz" ' Put your password here
For Each ws In Worksheets
   ws.Protect Password:=pwd, UserInterfaceOnly:=True
Next ws

End Sub

and to unprotect...

Sub UnProtect()

Dim ws As Worksheet
Dim pwd As String

pwd = "xyz" ' Put your password here
For Each ws In Worksheets
    ws.UnProtect Password:=pwd
Next ws

End Sub

The Protect macro works fine.

The UnProtect macro protects all worksheets, but i need it to ask the user for the password.

Can anyone help?

Upvotes: 0

Views: 2400

Answers (1)

pokemon_Man
pokemon_Man

Reputation: 902

Sub UnProtect()

Dim ws As Worksheet
Dim pwd As String
Dim myValue As Variant
pwd = "xyz" ' Put your password here
myValue = InputBox("What is the password?")
If myValue = pwd Then
    For Each ws In Worksheets
        ws.UnProtect Password:=pwd
    Next ws
Else
    'do nothing or msgbox
End If

End Sub

Upvotes: 2

Related Questions