Sheet Protection?

User avatar
Don Wells
5StarLounger
Posts: 689
Joined: 27 Jan 2010, 16:45
Location: Ottawa, Ontario, Canada

Sheet Protection?

Post by Don Wells »

I am running 2003 and am attempting to use the following code to determine whether the active sheet is an unprotected worksheet or not. The active sheet is a protected worksheet (as shown by the attached figure), yet the following function returns False. Any ideas?

Code: Select all

Public Function Cht_Or_Protected() As Boolean
Dim ws As Worksheet
  
  On Error Resume Next
  Set ws = ActiveSheet
  If Err Then
    On Error GoTo 0
    Cht_Or_Protected = True
    GoTo ExitHandler
  End If
  Cht_Or_Protected = ws.ProtectionMode
  
ExitHandler:
  Set ws = Nothing
End Function
You do not have the required permissions to view the files attached to this post.
Regards
Don

User avatar
Goshute
3StarLounger
Posts: 397
Joined: 24 Jan 2010, 19:43
Location: Salt Lake City, Utah, USA

Re: Sheet Protection?

Post by Goshute »

Don, without testing, I think you can Set a sheet as an Object in VBA, whether or not it's protected. How come you aren't using:

Worksheets(ActiveSheet.Name).ProtectContents
Goshute
I float in liquid gardens

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Sheet Protection?

Post by HansV »

According to the Excel VBA help, ProtectionMode returns something different from what you want:
True if user-interface-only protection is turned on. To turn on user interface protection, use the Protect method with the UserInterfaceOnly argument set to True. Read-only Boolean.
To test whether a sheet has been protected, use the ProtectContents property.
True if the contents of the sheet are protected. This protects the individual cells. To turn on content protection, use the Protect method with the Contents argument set to True. Read-only Boolean.
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Sheet Protection?

Post by HansV »

Goshute wrote:Worksheets(ActiveSheet.Name).ProtectContents
Or simply ActiveSheet.ProtectContents:

Code: Select all

Public Function Cht_Or_Protected() As Boolean
  Cht_Or_Protected = ActiveSheet.ProtectContents
End Function
Best wishes,
Hans

User avatar
Goshute
3StarLounger
Posts: 397
Joined: 24 Jan 2010, 19:43
Location: Salt Lake City, Utah, USA

Re: Sheet Protection?

Post by Goshute »

HansV wrote:
Goshute wrote:Worksheets(ActiveSheet.Name).ProtectContents
Or simply ActiveSheet.ProtectContents
True. For that matter I don't see a need for a function; it's a one-line test, and I rarely need to use it more than once in a sub.

EDIT - figured out why I used the longwinded Worksheets(ActiveSheet.Name).ProtectContents syntax - it was inside a loop of
ActiveWindow.SelectedSheets(intCounter), and within ActiveWindow you have to activate each sheet ... I think... :smile:
Last edited by Goshute on 22 Dec 2010, 20:39, edited 2 times in total.
Goshute
I float in liquid gardens

User avatar
Don Wells
5StarLounger
Posts: 689
Joined: 27 Jan 2010, 16:45
Location: Ottawa, Ontario, Canada

Re: Sheet Protection?

Post by Don Wells »

Hans and Goshute
    Thank you both
    It speaks well for your patience that you didn't reply with "RTFM". :thankyou:
Regards
Don