unprotecting multiple sheets with vba textbox

enicolas

New Member
Joined
Sep 16, 2011
Messages
2
Does anybody know how I can get my password to unprotect my multiple sheets. This is what I currently have.
When the workbook closes, it automatically protect all sheets with a specific code I've assigned.
I created a textbox and change the PasswardChar to "*" and I'm using the code below to unprotect the sheets. However, when I enter the password, I get an debug error message. I've tried different ways of using the code, but I can't get one to work. Please help... thank you!

****

Private Sub cmdOK_Click()
Dim WSheet As Worksheet
'Dim Pwd As String


For Each WSheet In Worksheets
If txtPassword = "Test123" Then
WSheet.Unprotect Password:=txtPassword.Value
End If
Next WSheet

If Err <> 0 Then
MsgBox "You have entered an incorect password. All worksheets could not " & _
"be unprotected. Very that the CAPS LOCK key is off.", vbCritical, "Incorect Password"
End If

On Error GoTo 0
Unload Me

End Sub


'The code below is what automatically protects the Sheets when the workbook is closed. This code is in the Workbook.


Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim i As Long
For i = 1 To Sheets.Count
Sheets(i).Protect Password:="Test123"
Next i

End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
change:
WSheet.Unprotect Password:=txtPassword.Value
to:
WSheet.Unprotect Password:="*"

and change:
Sheets(i).Protect Password:="Test123"
to:
Sheets(i).Protect Password:="*"

??

I think I may be on the wrong track..
 
Last edited:
Upvote 0
1. Where have you put the button (Userform? Sheet?)
2. When you get the error, what does it say?
3. When you then choose Debug, which line is highlighted in yellow?
4. Where have you put the code that causes an error?
 
Upvote 0
Hi, I have been working on this for most of last night... I am determined to get this figured out. So messing with it and thinking on it before I read your post I made the following changes to the variables in the userform and I got it to work... sort of. I am not getting the debug error message anymore, now I am able to unlock the sheets with any thing i enter into the textbox.

This is what I have now...

Private Sub cmdOK_Click()

Dim WSheet As Worksheet
Dim strMsg As String
'Dim Pwd As String
For Each WSheet In Worksheets
' If txtPassword = Pwd Then
WSheet.Unprotect Password:="Test123"
strMsg = "All sheets unprotected."
' ElseIf txtPassword <> "" Then
' strMsg = "You have entered an incorect password. All worksheets could not " & _
' "be unprotected. Very that the CAPS LOCK key is off."
' End If
Next WSheet
MsgBox strMsg

' If Err <> 0 Then

' End If
Unload Me
End Sub

I will try your suggestion and see if that helps... thank you in advance for your help. I would like to get this figure out no later than Monday... so I'm drinking lots of coffee :).
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,681
Members
452,937
Latest member
Bhg1984

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