Protecting Multiple Workbooks

Pam

New member
Joined
Nov 29, 2011
Messages
2
Reaction score
0
Points
0
Is there an easy way to protect multiple workbooks (over 500) without opening each one and selecting the option?

Same question for whether I could protect the worksheets within each workbook.

Thanks,
Pam
 

Ken Puls

Administrator
Staff member
Joined
Mar 13, 2011
Messages
2,520
Reaction score
6
Points
38
Location
Nanaimo, BC, Canada
Website
www.excelguru.ca
Excel Version(s)
Excel Office 365 Insider
Protecting the worksheets in each workbook, yes. Protecting each workbook, no. Each would need to be opened and saved, setting the password in the process.

To protect each worksheet in an open workbook though... that's actually pretty easy. What version of Excel are you using?
 

Ken Puls

Administrator
Staff member
Joined
Mar 13, 2011
Messages
2,520
Reaction score
6
Points
38
Location
Nanaimo, BC, Canada
Website
www.excelguru.ca
Excel Version(s)
Excel Office 365 Insider
At least, I should probably clarify that...

Yes, either can actually be done. There's more scripting involved with opening each workbook in a folder, but it is possible.

Are all the workbooks in one folder, or a couple of folders?
 

Pam

New member
Joined
Nov 29, 2011
Messages
2
Reaction score
0
Points
0
There will eventually be 1000+ files (that we are creating via a script, and prepopulating some of the data). 2 of 10 sheets are prepopulated so the base file that we are using needs these to be unprotected (and the workbook). The 1000 files will be in ~12 folders (created into these folders via the same script that is prepopulating the data).

Pam
 

Ken Puls

Administrator
Staff member
Joined
Mar 13, 2011
Messages
2,520
Reaction score
6
Points
38
Location
Nanaimo, BC, Canada
Website
www.excelguru.ca
Excel Version(s)
Excel Office 365 Insider
If you're creating the files via a script, I can't see any reason why you couldn't script the protection for both sheets and workbook names. In order to prepopulate the files with data, you have to have it open, so you could apply protection then.

Just as an interesting aside, you can also protect all worksheets so that users can't access the data, but macros can. The only rub is that the setting to do that is not persistent so needs to be set each time the workbook is opened. (Which can also be scripted.)

For reference, the following code, saved to your personal workbook, would allow you to protect all worksheets in the active workbook:

Code:
Public Sub ProtectAllSheets()
    Dim pWrd As Variant
    Dim ws As Worksheet
    
    'Ask the user for the password
    pWrd = CStr(InputBox("Please enter the password to use", "Enter password"))
    
    'Check what to do if password is blank or omitted
    If Len(pWrd) = 0 Then
        Select Case MsgBox("No password provided.  Protect sheets with no password?", _
            vbYesNoCancel, "I'm confused")
            Case Is = vbNo, vbCancel
                Exit Sub
            Case Else
                'User wants blank password
        End Select
    End If
    
    'Protect the worksheets
    For Each ws In ActiveWorkbook.Worksheets
        ws.Protect Password:=pWrd
    Next wsEnd Sub

To do the workbook open one though... I'd be curious to learn a little more about the coding that you're currently doing.
 
Top