Protecting and Unprotecting more than 1 worksheets

J

Jeffrey

Hi all. Is it possible to protect and unprotect more than 1
worksheets. Say the first 4 worksheet in a workbook using VBA codes?
Thanks much.

Jeff
 
G

Gord Dibben

Sub Protect_Some_Sheets()
Application.ScreenUpdating = False
Dim N As Single
For N = 1 To 4
'for all sheets use N = 1 to Sheets.Count
Sheets(N).Protect Password:="justme"
Next N
Application.ScreenUpdating = True
End Sub


Gord Dibben MS Excel MVP
 
G

Gord Dibben

Or this for selected sheets.

Sub Protect_Selected_Sheets()
Set MySheets = ActiveWindow.SelectedSheets
For Each ws In MySheets
ws.Select
ws.Protect Password:="justme"
Next ws
End Sub


Gord
 
M

Matthew Norman

Hi all. Is it possible to protect and unprotect more than 1
worksheets. Say the first 4 worksheet in a workbook using VBA codes?
Thanks much.

Jeff

it is very simple. loop through the sheets protecting each one in turn

Eg:

Sub Doit()
i = 1
Do Until i = 4
Sheets(i).Activate
ActiveSheet.Protect

i = i + 1
Loop

End Sub



End Sub
 
J

Jeffrey

Or this for selected sheets.

Sub Protect_Selected_Sheets()
Set MySheets = ActiveWindow.SelectedSheets
For Each ws In MySheets
 ws.Select
   ws.Protect Password:="justme"
Next ws
End Sub

Gord







- Show quoted text -

Hi Gord,

Thanks much. This works very well.

Cheers.

Jeff
 

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