Loop to PW Protect files in Folder with Unique PWs from list

Von Julmust

New Member
Joined
Jan 4, 2023
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have 97 files in a specific folder, I would like to password protect each of these files with different passwords (ideally using a list).

I have a Worksheet in a Workbook that lists all the files in Column A and then the password in Column B.

Any ideas how to create this loop would be appreciated (an automatic way to generate a list of files in folder for a Worksheet would be neat too, I have some pretty badly written VBA that does this already).

Best,
V
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi. This takes about 2-3 seconds per file for my testing. I'd imagine even longer on a network path. Using this method, each file is opened and the password is applied prior to closing. There is also a sub to grab all the file names from the specific folder. Notice the constant strings used for the folder path and the worksheet to save the filenames to.

VBA Code:
'Update the below constants
'oPath will be the path with your 97 files - remember to
'include the slash at the end
Const oPath As String = "C:\Users\reyno\Documents\97Files\"
'sName will be the sheet name of the sheet with the filenames
'and passwords
Const sName As String = "Sheet1"

Sub getfilenames()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets(sName)
Dim oFSO As Object: Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim oFolder As Object: Set oFolder = oFSO.getfolder(oPath)
Dim oFile As Object, i As Integer

'set your first row - if you have a header use 2
i = 2

'loops through oFolder and adds each filename to sName, column A
For Each oFile In oFolder.Files
    ws.Cells(i, 1) = oFile.Name
    i = i + 1
Next oFile

End Sub

Sub loopandlock()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets(sName)
Dim lrow As Long: lrow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
Dim c As Range, pw As String, xWB As Workbook

'Optimize processing
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
End With

'error handling
On Error GoTo rThings

'loops through workbook names in column A, beginning on row 2
For Each c In ws.Range("A2:A" & lrow).Cells
    If Not ws.Cells(c.Row, 2).Value = vbNullString Then
    'As long as there's a password on the same row, code runs
        'sets password based on current row's column B
        pw = ws.Cells(c.Row, 2).Value
        'opens workbook
        Set xWB = Workbooks.Open(Filename:=oPath & c.Value, ReadOnly:=False)
        Application.DisplayAlerts = False
        'saves with the applicable password and then closes
        With xWB
            .Activate
            .SaveAs oPath & c.Value, , pw
            .Close
        End With
        Application.DisplayAlerts = True
    End If
Next c

'Restores settings
With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
End With

'Task completion message
MsgBox "The task has completed.", vbInformation, "Task Completion"

Exit Sub

'Error Handling
rThings:
MsgBox "The below error has occurred: " & vbCrLf & vbCrLf & "Error Number:" & Err.Number & vbCrLf & _
    "Error Description: " & Err.Description
With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,249
Messages
6,123,882
Members
449,130
Latest member
lolasmith

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