How do you Determine if a Worksheet is protected?

N

nesmith6866

I have one sub routines that is called from several locations in my VBA code;
sometimes the worksheet is already unprotected when the sub routine is called
and sometimes it is not. The problem is that my sub routine makes changes
to the sheet and reprotects. When this is called on by code that already
unprotected, then the sheet is protected after the routine is called and the
next line that edits the sheet crashes. I cannot remove the protect
statement from the sub routine, because it would occasionally cause the sheet
to remian unprotected. Is there a way for the sub routine to check to see if
the worksheet is protected so I can add an if statement to check this. if
the worksheet is protected when the sub routine is called, then it will
unprotect, make changes, and re-protect; if it is not protected, then it will
leave the sheet unprotected.
 
M

Mike H

Hi,

Something like this

For X = 1 To Worksheets.Count
If Sheets(X).ProtectContents = True Then
MsgBox "SheetProtected"
Else
MsgBox "SheetNotProtected"
End If
Next

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top