<FWIW>
Here's a trimmed down version of what I use for setting generic sheet
protection. It allows making changes via code without having to toggle
protection off/on. Unfortunately, the parameter that makes this
possible (UserInterfaceOnly) does not persist between runtimes and so
protection must be reset every time the workbook is opened, by running
the 'ResetProtection' routine at startup from the Workbook_Open event
or the Auto_Open sub...
Public Const PWD$ = "123" '//edit to suit
Sub wksProtect(Optional Wks As Worksheet)
' Protects specified sheets according to Excel version.
' Assumes Public Const PWRD as String contains the password, even if
there isn't one.
'
' Arguments: Wks [In] Optional. Ref to the sheet to be protected.
' (Defaults to ActiveSheet if missing)
If Wks Is Nothing Then Set Wks = ActiveSheet
On Error Resume Next
With Wks
If Val(Application.VERSION) >= 10 Then
'Copy/paste the desired parameters above the commented line.
.Protect Password:=PWRD, _
DrawingObjects:=True, Contents:=True, Scenarios:=True, _
UserInterfaceOnly:=True, _
AllowFiltering:=True, _
AllowFormattingColumns:=True, _
AllowFormattingRows:=True, _
AllowFormattingCells:=True, _
AllowDeletingColumns:=True, _
AllowDeletingRows:=True ', _
AllowInsertingColumns:=True, _
AllowInsertingHyperlinks:=True, _
AllowInsertingRows:=True, _
AllowUsingPivotTables:=True
Else
.Protect Password:=PWRD, _
DrawingObjects:=False, Contents:=True, Scenarios:=True, _
UserInterfaceOnly:=True
End If
' .EnableAutoFilter = True
.EnableOutlining = True
' .EnableSelection = xlNoRestrictions
.EnableSelection = xlUnlockedCells
' .EnableSelection = xlNoSelection
End With
End Sub
Sub ResetProtection(Optional Wks As Worksheet)
If Wks Is Nothing Then Set Wks = ActiveSheet
Wks.Unprotect PWRD: wksProtect Wks
End Sub
To use for a single sheet named "Sheet1" (as opposed to all sheets)...
ResetProtection Sheets("Sheet1")
To use at startup...
Call ProtectAllSheets
Sub ProtectAllSheets()
Dim wks As Worksheet
For Each wks In ThisWorkbook.Worksheets
ResetProtection wks
Next 'wks
End Sub
Note that I have configured the 'wksProtect' procedure to apply your
posted settings by including all the desired options above the comment
flag (apostrophe after 'AllowDeletingRows').
How this works is by shifting the parameters around so those that you
want to apply are above the commented out parameters. I no longer use
this approach in non-trivial projects since I have developed a more
efficient methodology that stores protection settings in a local scope
defined name for sheets that require protection. This allows me to
customize the protection parameters for each sheet specific to
context/need as opposed to a generic setting for all sheets. If anyone
is interested in going with such an approach I can post details on
request...
--
Garry
Free usenet access at
http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion