protecting, unprotecting workbook macro error

mike_ate_a_pie

Board Regular
Joined
Sep 25, 2009
Messages
69
i have this code found on the net for protecting/unprotecting my workbook, however i am unable to work out the password, i have tried Password Input and changing it to other passwords but cant figure out what i'm missing.

sure it must be simple but just cant see it,

any help appreciated

thanks


Option Explicit

Sub ProtectAll()

Dim wSheet As Worksheet
Dim Pwd As String

Pwd = InputBox("Enter your password to protect all worksheets", "Password Input")
For Each wSheet In Worksheets
wSheet.Protect Password:=Pwd
Next wSheet

End Sub

Sub UnProtectAll()

Dim wSheet As Worksheet
Dim Pwd As String

Pwd = InputBox("Enter your password to unprotect all worksheets", "Password Input")
On Error Resume Next
For Each wSheet In Worksheets
wSheet.Unprotect Password:=Pwd
Next wSheet
If Err <> 0 Then
MsgBox "You have entered an incorect password. All worksheets could not " & _
"be unprotected.", vbCritical, "Incorect Password"
End If
On Error Goto 0

End Sub </pre>
 
if you are running other macros that need to edit cells that are protected you will need to unprotect the sheets first.

Add the unprotect macro to the start of the desired macro and the protect macro to the end.
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
This is my code, is there a easy way to change this so it works when the workbook is protected?

Sub Button22_Click()
If Range("M:N").ColumnWidth = 0 Then
Range("M:N").ColumnWidth = 11.43
Else
Range("M:N").ColumnWidth = 0
End If
End Sub

Thanks again,
 
Upvote 0
I also use the macro above to protect, unprotect workbooks.

How do I adjust the "protect" macro to tick the box that allow users to

"select locked cells"
"select unlocked cells"
 
Upvote 0
Selection.Locked = False
or
Selection.Locked = True

Is this what you mean?
 
Upvote 0
No.

In the protect sheet box where the password is entered, there is also an option: "allow all users of this worksheet to" tick list of what you want protected (15 items altogether).

I would like the macro to tick the 1st (select locked cells) and 2nd (select unlocked cells) box.

Thanks
 
Upvote 0
Check out the EnableSelection property of the Worksheet object. From Help:

EnableSelection Property

</OBJECT>
Returns or sets what can be selected on the sheet. Can be one of the following XlEnableSelection constants: xlNoRestrictions, xlNoSelection, or xlUnlockedCells. Read/write Long.

Remarks

This property takes effect only when the worksheet is protected: xlNoSelection prevents any selection on the sheet, xlUnlockedCells allows only those cells whose Locked property is False to be selected, and xlNoRestrictions allows any cell to be selected.
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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