how to check if a sheet is protected

syaronc

New Member
Joined
Jun 2, 2004
Messages
16
:oops:
Hi,
I have a workbook with data I need to hide from some users (money...) and I am trying to create a macro that when I exit the workbook some columns while "Hide" and the sheet will be password protected. I have managed to do all but I don't know how to check if the sheet is protected, because if I try to run my macro when the sheet is protected I get an error massage
Please help...
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Welcome to the Board!

Why not just Unprotect it,do your thing and reprotect it?

<font face=Tahoma>    ActiveSheet.Unprotect Password:="password"
    <SPAN style="color:#007F00">'   Do your thing here</SPAN>
    ActiveSheet.Protect Password:="password"
</FONT>

Hope that helps,

Smitty
 
Upvote 0
Hi,
you could also protect the sheet so that it is only protected in the user interface but still allows your macros or code to work with it.

Code:
     ActiveSheet.Protect Password:="myPass", UserInterfaceOnly:=True
 
Upvote 0
I think Smitty and Rotarinn have you covered for what you want, but here is the answer to your actual question.

Code:
MsgBox Sheets("Sheet1").ProtectContents

or

Code:
x = Sheets("Sheet1").ProtectContents

This will return True or False for if the sheet is protected or not.
 
Upvote 1
This further to your actual question:

Sub Test1()
If ActiveSheet.ProtectContents = True Then
MsgBox "Protected"
Else
MsgBox "Not protected"
End If
End Sub


or this function and MsgBox from Goblin:

Public Function IsSheetProtected(Optional cell As Range, _
Optional volatile As Boolean = True) As String
Dim sh As Worksheet
Application.volatile volatile 'Updated on every recalc if true
If cell Is Nothing Then
Set sh = ActiveSheet
Else
Set sh = cell.Parent
End If
IsSheetProtected = "Unprotected"
If sh.ProtectContents = True Then
sh.Unprotect
If sh.ProtectContents = True Then
IsSheetProtected = "Password protected"
Exit Function
End If
sh.Protect
IsSheetProtected = "Protected"
End If
End Function

Sub Test2()
MsgBox IsSheetProtected
End Sub
 
Upvote 0
Just nitpicking Tom because I can :)

Code:
Sub Test1() 

If ActiveSheet.ProtectContents Then 
    MsgBox "Protected" 
    Else 
    MsgBox "Not protected" 
End If 

End Sub
 
Upvote 0
I just changed
Code:
If ActiveSheet.ProtectContents = True Then
To
Code:
If ActiveSheet.ProtectContents Then

Since the = True is redundant
 
Upvote 0
Well then you are doing the opposite of nitpicking, which is cutting a corner. It's a better practice to go the distance and not cut corners by assuming that a default is going to evaluate to True in all versions (including future versions) in all cases. As new versions of Office apps are released, MS also releases a new version of the object model, which you can see for yourself in the Available References pane. In the context of other macros and code going on among different versions using your macros, why take the chance and not specify something to be True even if it "should be". It adds no overhead weight to the file and is easier to rule out when a bug does occur. Your call, but it's an unnecessary risk in my opinion.
 
Upvote 0

Forum statistics

Threads
1,214,980
Messages
6,122,563
Members
449,088
Latest member
Motoracer88

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