VBA - How to check if a sheet is protected?

hockeypuck

New Member
Joined
May 30, 2006
Messages
4
How can I check if a sheet is protected?
This code below gives me a reply but then it also goes ahead and protects the sheet. I just want to check - not protect.

Private Sub Worksheet_Change(ByVal Target As Range)
If (ActiveSheet.Protect=true) Then MsgBox "Is protected"
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
That's because you are protecting the sheet within the If statement itself. ActiveSheet.Protect = True does just that--protects the sheet.

You want ProtectContents:
Code:
If ActiveSheet.ProtectContents = True Then
    MsgBox "Is protected"
Else
    MsgBox "Not protected"
End If
 
Upvote 0
What I really want to do is:

If ActiveSheet.Protect=true Then ActiveSheet.Protect userinterfaceonly:=True

If the sheet is protected (no password) then let macros run on the sheet.
If the sheet is unprotected then don't protect it.
 
Upvote 0
So, just change the If statement (untested):

Code:
If ActiveSheet.ProtectContents = True Then ActiveSheet.Protect UserInterfaceOnly:=True
 
Upvote 0

Forum statistics

Threads
1,223,635
Messages
6,173,479
Members
452,516
Latest member
archcalx

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