View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.misc
GS[_2_] GS[_2_] is offline
external usenet poster
 
Posts: 3,514
Default Qualifying permissions in protect unprotect macro.

I forgot to post a sample of how to protect...

Dim wks As Worksheet
For Each wks In ActiveWorkbook.Worksheets
wksProtect wks.Name
Next

OR
wksProtect "Sheet1" 'pass the sheetname

OR
wksProtect 'to apply to activesheet

I have a revised version of wksProtect that takes a ref to the sheet
instead of a sheetname, so I can ref any sheet in any open workbook.
For example:

To protect all sheets in a workbook...
Dim wks As Worksheet
For Each wks In Workbooks("Book1").Worksheets
wksProtect wks
Next

OR
To protect a single sheet...
wksProtect Workbooks("Book1").Sheets(1)

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. 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:=False, _
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 'wksProtect()

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc