简体   繁体   中英

Excel VBA to password lock multiple workbooks based on cell value

I have a folder with 400 workbooks. I want each to have a different password. I found some code that looks like it should work ... but nothing happens at all.

Sub YE_SetPassword()

Dim strFile As String
Dim strPath As String
Dim colFiles As New Collection
Dim i As Integer
Dim x As String
Dim ws As Worksheet

strPath = "C:\PATH"
strFile = Dir(strPath)

' Add Excel File Names to the variable colfiles
While strFile <> ""
    colFiles.Add strFile
    strFile = Dir
Wend

' Start reading colfiles collection and open workbooks one at a time
If colFiles.Count > 0 Then
    For i = 1 To colFiles.Count
        ActiveSheet.Cells(i, 1).Value = colFiles(i)
            Application.Workbooks.Open strPath & colFiles(i)
        Workbooks(colFiles(i)).Activate

' Once workbook is open search for Sheet2 and Sheet3 and if they are there, delete them
Application.DisplayAlerts = False
Err.Clear
On Error Resume Next
    Set ws = Sheets("Sheet2")
    ws.Delete
Err.Clear
On Error Resume Next
    Set ws = Sheets("Sheet3")
    ws.Delete
Application.DisplayAlerts = True

' Check cell value of A2 for name the assign a password based on that value
    x = Range("A2").Value
    Select Case x
    Case "LOOK FOR THIS NAME"
        pw = "USE THIS PWD"
    End Select

    ' Save the workbook with unique password
    ActiveWorkbook.SaveAs Filename:= _
        strPath & colFiles(i), FileFormat:= _
            xlOpenXMLWorkbook, Password:=pw, WriteResPassword:="", _
            ReadOnlyRecommended:=False, CreateBackup:=False
        ActiveWorkbook.Close True
    Next i
End If

End Sub

I am not sure what is going wrong here. No errors come up, but nothing happens. I did try removing some steps, still nothing. Any suggestions? Hoping to not have to lock these manually, even if it is a pain to write the Case statements.

Alternatively, is there some way to point the macro to a lookup sheet and have it pick passwords based on the lookup? I am looking up names and locking sheets with the last 4 of a ID#.

The code above does work. I made a mistake in the path, omitting the final "\\". Thank you to chris neilsen and BigBen for pointing me in the right direction!

EDIT: That said, as I worked more on this, I found a much simpler way to go about it. Below is code to lock and unlock a folder full of workbooks based on looking up values in the currently active workbook.

Sub LockFolder()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Page1")
Dim Loc As String: Loc = "C:\PATH\"
Dim pw As String, fn As String, cb As Workbook, i As Long

'Loc = Local Location
'pw = Password
'fn = File Name
'cb = Current Book

Application.ScreenUpdating = False
Application.DisplayAlerts = False
    For i = 2 To ws.Range("A" & ws.Rows.Count).End(xlUp).Row
        On Error Resume Next 'If book does not exist
            fn = Loc & ws.Range("A" & i)
            pw = ws.Range("B" & i)

            Set cb = Workbooks.Open(fn, Password:="")

            cb.SaveAs fn, Password:=pw
            cb.Close False 'You just saved the book above, no need for TRUE
        On Error GoTo 0
    Next i
Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub


Sub Unlock_folder()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Page1")
Dim Loc As String: Loc = "C:\PATH\"
Dim pw As String, fn As String, cb As Workbook, i As Long

'Loc = Local Location
'pw = Password
'fn = File Name
'cb = Current Book

Application.ScreenUpdating = False
Application.DisplayAlerts = False
    For i = 2 To ws.Range("A" & ws.Rows.Count).End(xlUp).Row
        On Error Resume Next 'If book does not exist
            fn = Loc & ws.Range("A" & i)
            pw = ws.Range("B" & i)

            Set cb = Workbooks.Open(fn, Password:=pw)

            cb.SaveAs fn, Password:=""
            cb.Close False 'You just saved the book above, no need for TRUE
        On Error GoTo 0
    Next i
Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM