harveya915
Board Regular
- Joined
- Sep 4, 2015
- Messages
- 141
I have a button assigned to open up a UserForm with a text box. I sort of have it set up so that when you input the unprotect pword in to that text box, then it unprotects the sheet. However I have an issue that when the incorrect pword is entered it gives me a run time error. Can someone help me out with this? below is the code I am using.
The debugger highlights the row that is in red.
Just a little heads up, I am new to this VBA. I found this code somewhere on here, but tweaked it a bit so that it meets my needs. So some things may seem impractical where I use them.
Code:
Private Sub CommandButton7_Click()
Dim wSheet As Worksheet
Dim pword As String
pword = "2019"
If pword <> TextBox2.Text Then MsgBox "Invalid Password"
For Each wSheet In Worksheets
If wSheet.ProtectContents = True And TextBox2.Text = 2019 Then
wSheet.Unprotect Password:=2019
Else
[COLOR=#ff0000]wSheet.Unprotect Password:=TextBox2.Text[/COLOR]
End If
Next wSheet
Unload UserForm3
End Sub
The debugger highlights the row that is in red.
Just a little heads up, I am new to this VBA. I found this code somewhere on here, but tweaked it a bit so that it meets my needs. So some things may seem impractical where I use them.
Last edited: