MsgBox to tell if worksheet is protected or not protected

Cosmos75

Active Member
Joined
Feb 28, 2002
Messages
359
Here's what I have, am trying to run code depending on whether or not the worksheet is protected.

Sheets("SheetName").Select

If ActiveSheet.Unprotect = True Then
MsgBox ("Not Protected")
'Run Code 1 Here
Exit Sub
End If

If ActiveSheet.Protect = True Then
MsgBox ("Protected")
'Run Code 2 here
Exit Sub
End If

but this only seems to protect the sheet if it isn't protected and vice versa...
:(
 

Some videos you may like

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

dk

MrExcel MVP
Joined
Feb 15, 2002
Messages
2,942
Hi,

Your code is running the Protect method. You are not checking to see whether or not the sheet is protected. To do that, try something like this:-

Code:
    Sheets("SheetName").Select

    If Sheet1.ProtectContents = False Then
        MsgBox ("Not Protected")
        'Run Code 1 Here
        Exit Sub
    End If

    If ActiveSheet.ProtectContents = True Then
        MsgBox ("Protected")
        'Run Code 2 here
        Exit Sub
    End If
 

Cosmos75

Active Member
Joined
Feb 28, 2002
Messages
359
Thanks!!

Ended up with this!

If Sheets("Permitted Constituents").ProtectContents = True Then
MsgBox "Sheet is protected"
'Code 1 here
Else
MsgBox "Sheet is unprotected"
'Code 2 here
End If
 

Watch MrExcel Video

Forum statistics

Threads
1,122,371
Messages
5,595,784
Members
414,020
Latest member
Meghdad

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
Top