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