ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How do you Determine if a Worksheet is protected? (https://www.excelbanter.com/excel-programming/436887-how-do-you-determine-if-worksheet-protected.html)

nesmith6866

How do you Determine if a Worksheet is protected?
 
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.

Ron de Bruin

How do you Determine if a Worksheet is protected?
 
You can test it like this

If ActiveSheet.ProtectContents = True Then



--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"nesmith6866" wrote in message ...
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.


Mike H

How do you Determine if a Worksheet is protected?
 
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

"nesmith6866" wrote:

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.



All times are GMT +1. The time now is 12:40 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com