Protect/Unprotect Password via userform

Donai

Well-known Member
Joined
Mar 28, 2009
Messages
543
hi, i have created a userform to unprotect/protect all worksheets in a workbook, so in the userform i have a textbox and a command button, in the userform code i have this, the problem is where do i put the password in this code?


Private Sub CommandButton1_Click()
Dim wSheet As Worksheet
For Each wSheet In Worksheets
If wSheet.ProtectContents = True Then
wSheet.Unprotect Password:=TextBox1.Text
Else
wSheet.Protect Password:=TextBox1.Text
End If
Next wSheet
Unload Me
End Sub


</PRE>


</PRE>
 
Last edited:
Yay, this works mate. Thanks Smitty

Try this:

Private Sub CommandButton1_Click()
Dim wSheet As Worksheet
Dim pword As String
pword = "yada"

If pword <> TextBox1.Text Then
MsgBox "No Pass", vbCancel
Exit Sub
Else:
For Each wSheet In Worksheets
If wSheet.ProtectContents = True Then
wSheet.Unprotect Password:=pword
Else
wSheet.Protect Password:=pword
End If
Next wSheet
End If

Unload Me

End Sub
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hey,

I'm trying to find a way to use the "ActiveSheet.Unprotect" method. However, if 'Cancel' is clicked or the password is invalid, the macro simply errors. How do you loop the password input prompt or exit the sub?
 
Upvote 0

Forum statistics

Threads
1,215,573
Messages
6,125,608
Members
449,238
Latest member
wcbyers

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top