0
votes

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?

1
Look into InputBox()Scott Craner
@ScottCraner sorry... edited. I need help in asking the user for a password to unprotect all worksheets.A P
And I put how to do that in my new comment. vba has InputBox just for that sort of thing.Scott Craner

1 Answers

2
votes
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