Macro protects sheet with password but can unprotect without password

cde

New Member
Joined
Jul 1, 2009
Messages
13
I have created the below macro (which fires from a button) that loops through all sheets in a workbook and will either protect or unprotect the worksheets with a given password designated from an input cell. This macro works fine - it works exactly as it should.

The problem is when I go to a specific sheet, I can unprotect the sheet without any password. If I want to give full access to a specific sheet, I still want a user to have to input a password. What is my code missing?

Thanks in advance for your help.
===========================================

Private Sub CommandButton1_Click()

Dim wSheet As Worksheet

For Each wSheet In Worksheets

TextBox1.Text = ThisWorkbook.Sheets("Macros").Range("E3")

If wSheet.ProtectContents = True Then
wSheet.Unprotect Password:=TextBox1.Text
Else
wSheet.Protect Password:=TextBox1.Text
wSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingColumns:=True, AllowFormattingRows:=True
ActiveSheet.EnableSelection = xlUnlockedCells

End If
Next wSheet
Unload Me
End Sub
 
Try

Code:
Private Sub CommandButton1_Click()

Dim wSheet As Worksheet

For Each wSheet In Worksheets

TextBox1.Text = ThisWorkbook.Sheets("Macros").Range("E3")

If wSheet.ProtectContents = True Then
wSheet.Unprotect Password:=TextBox1.Text
Else
wSheet.Protect Password:=TextBox1.Text, DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingColumns:=True, AllowFormattingRows:=True
ActiveSheet.EnableSelection = xlUnlockedCells

End If
Next wSheet
Unload Me
End Sub
 
Upvote 0
Peter,
Awesome!! It worked like a champ. Thank you for the help and such a quick response. Its guys like you that make this community so great!

One question: what was I missing? I'm having a hard time seeing it in the code, and where I was going wrong. I would like to understand this better.

Thanks again for all the help.

(For future reference in case some else looks at this post, I am using excel 2003.)
 
Upvote 0
Here

Code:
wSheet.Protect Password:=TextBox1.Text
wSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingColumns:=True, AllowFormattingRows:=True
ActiveSheet.EnableSelection = xlUnlockedCells

you were protecting with a password but then protecting without a password.
 
Upvote 0

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