Another newbie question - Passwordprotect SOME sheets Macro

dugong

New Member
Joined
Oct 20, 2006
Messages
36
Office Version
  1. 365
Platform
  1. MacOS
Hello again

I have seen the macro that password-protects ALL sheets in an excel file, i.e.:

Sub ProtectAllSheets()
'Protect sheets - using password
Dim ws As Worksheet
Dim sOrigSheet As String
Dim sOrigCell As String
Dim J As Integer

Application.ScreenUpdating = False
sOrigSheet = ActiveSheet.Name
sOrigCell = ActiveCell.Address

For Each ws In Worksheets
ws.Select
ws.Protect Password:="test"
Next ws

Application.GoTo Reference:=Worksheets("" & sOrigSheet & "").Range("" & sOrigCell & "")
Application.ScreenUpdating = True
End Sub


How do you adapt it to password-protect only say the 3,4,5 & 9 sheets in an 11-sheet file?

Thanks again - and apologies if these seem dumb questions :rolleyes: I will also do a search in case it has been answered previously.

Dugong
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Something like

For Each ws In Worksheets
If ws.Name = "Sheet1" Or ws.Name = "Sheet2" Or ws.Name = "Sheet3" Then

ws.Select
ws.Protect Password:="test"
End If

Next ws
 
Upvote 0
.. or perhaps this structure?

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> ProtectSome()
    <SPAN style="color:#00007F">Dim</SPAN> SheetToProtect(3) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
    
    SheetToProtect(1) = "Sheet1"
    SheetToProtect(2) = "Sheet5"
    Sheet<SPAN style="color:#00007F">To</SPAN>Protect(3) = "Sheet7"
    <SPAN style="color:#00007F">For</SPAN> i = 1 To 3
        Worksheets(SheetToProtect(i)).Protect Password:="test"
    <SPAN style="color:#00007F">Next</SPAN> i
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Thank you.. I will try these suggestions :LOL:

Thanks for taking the time to reply.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,818
Members
449,049
Latest member
cybersurfer5000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top