VBA Password protecting selected multiple sheets

Nyanko

Active Member
Joined
Sep 1, 2005
Messages
437
Hi,

I'm trying to write a macro to help me manage protecting the excel files I work with. I want to be able to select (group) a selection of sheets then press a button to password protect only those sheets then ungroup them.

I will be using it as a personal macro rather than tied to a specific file so I can't assign named sheets to an array etc...

I know that because the sheets are grouped it's causing the protect routine to fail. Any suggestions for how to get round this ?

Code:
For Each sht In ActiveWindow.SelectedSheets
    sht.Protect Password:=strPassword
  Next sht

Can I assign the selected (grouped) sheets to an array and then loop through to protect ?
How do I change the "NameSheet","NamedSheet2" to only the selected sheet names ?

Code:
Set group = Sheets(Array("Sheet1", "Sheet2", "Sheet3"))
   For Each sht In group
     sht.Activate
     ActiveSheet.Protect "Password"
Next sht

Any advise would be welcome
 
Last edited:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Can I assign the selected (grouped) sheets to an array and then loop through to protect ?

Hi, here's one way maybe:

Code:
Sub ProtectSheets()
Dim Shts() As Long, i As Long
ReDim Shts(1 To ActiveWindow.SelectedSheets.Count)
For i = 1 To UBound(Shts)
    Shts(i) = ActiveWindow.SelectedSheets(i).Index
Next i
ActiveWindow.SelectedSheets(1).Select
For i = 1 To UBound(Shts)
    Sheets(Shts(i)).Protect "Password"
Next i
End Sub
 
Upvote 0
That is perfect !

I've never worked with Bound values before. I guess that's next on my research.

Thank you so much for this :)
 
Upvote 0
Hi, here's one way maybe:

Code:
Sub ProtectSheets()
Dim Shts() As Long, i As Long
ReDim Shts(1 To ActiveWindow.SelectedSheets.Count)
For i = 1 To UBound(Shts)
    Shts(i) = ActiveWindow.SelectedSheets(i).Index
Next i
ActiveWindow.SelectedSheets(1).Select
For i = 1 To UBound(Shts)
    Sheets(Shts(i)).Protect "Password"
Next i
End Sub

Now ... I may be pushing it.
But can I take the names of the sheets that were protected and display in a message box at the the end : "The following sheets were protected : SheetName, SheetName2 etc..."
 
Upvote 0
Hi, give this a try:

Code:
Sub ProtectSheets()
Dim Shts() As String, i As Long
ReDim Shts(1 To ActiveWindow.SelectedSheets.Count)
For i = 1 To UBound(Shts)
    Shts(i) = ActiveWindow.SelectedSheets(i).Name
Next i
ActiveWindow.SelectedSheets(1).Select
For i = 1 To UBound(Shts)
    Sheets(Shts(i)).Protect "Password"
Next i
MsgBox "The following sheets were protected: " & Join(Shts, ", ")
End Sub
 
Upvote 0
That's beautiful !

I'm going to enjoy working out how it's done next ;)

Thank you so much
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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