Fancier Protect Sheets, Workbooks
You should be able to use the code as is, without the use of Next ws - just
use Next
Make sure that you have Option Explicit at the top of the module. You may
not have closed all your loops (For....Next, If....End If). Excel will help
you find them....
another approach is to loop through the sheets by number
Dim x as Integer
For x = 1 to ActiveWorkbook.Worksheets.Count
If Sheets(x).ProtectContents = True Then
Sheets(x).Unprotect
MsgBox "unProtect Sheet(s)"
Else
Sheets(x).Protect
MsgBox "Protect Sheet(s)"
End If
Next
==================================
Sub ShtProtecter()
Dim sht As Worksheet
Application.ScreenUpdating = False
For Each sht In ActiveWorkbook.Worksheets
If sht.ProtectContents = True Then
sht.Unprotect
MsgBox "unProtect Sheet(s)"
Else
sht.Protect
MsgBox "Protect Sheet(s)"
End If
Next
Application.ScreenUpdating = True
End Sub
=============================
--
steveB
Remove "AYN" from email to respond
"John F." wrote in message
...
Thanks Steve Bell;
I gave it a good try, but am getting stuck.
The way I'm trying to put this together with your sample,
I get an error message at "next ws".
Its looking for another "For".
Any ideas?
"STEVE BELL" wrote:
See if you can fit this into your need...
=================================
Sub ShtProtecter()
Dim sht As Worksheet
Application.ScreenUpdating = False
For Each sht In ActiveWorkbook.Worksheets
If sht.ProtectContents = True Then
sht.Unprotect
MsgBox "unProtect Sheet(s)"
Else
sht.Protect
MsgBox "Protect Sheet(s)"
End If
Next
Application.ScreenUpdating = True
End Sub
--
steveB
Remove "AYN" from email to respond
"John F." <John wrote in message
...
I set up the following, attached to Icons on personal tool bar
( most is from this forum)
' ProtectionOff Macro
' Keyboard Shortcut: Ctrl+Shift+U
Dim ws As Worksheet
For Each ws In Worksheets
ws.Unprotect ("ABC")
Next ws
ActiveWorkbook.Unprotect (["DEF"])
End Sub
I have another one to PROTECT all sheets and workbook.
Questions:
1: Is there an icon I can use to Toggle between Protect / Unprotect?
(Instead of a seperate icon for each instruction)
2: Can the above be modified to check each sheets state before trying
to
change it?
(It might be in the correct state already)
I'm afraid that trying to, for example, protect a sheet that is
protected already,
might cause a glitch, under certain circumstances.
3: Is there a way to visually and easily show the state of the active
sheet(s)
and workbook, (whether its protected or not), Perhaps by colour on
the
icon(s)?
Or by having a message box pop up ( and fade away )
or ???????????
Thanks in advance for any help.
|