Password Protect Multiple Excel Workbook

heartyy

New Member
Joined
Feb 20, 2023
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi everyone - I was wondering does anyone have a script that would password protect multiple workbook all at one time? It's so time consuming to do it one by one. Please help. Thank you.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
You can start with this. Test on copy.
VBA Code:
Sub PasswordProtectMultipleWorkbooks()
    Dim folderPath As String
    Dim password As String
    Dim fileName As String
    Dim wb As Workbook

    ' Set the folder path and password
    folderPath = "C:\path\to\your\directory\"
    password = "yourpassword"

    ' Check if the folder path ends with a backslash, if not add it
    If Right(folderPath, 1) <> "\" Then
        folderPath = folderPath & "\"
    End If

    fileName = Dir(folderPath & "*.xls*")
    Do While fileName <> ""
        Set wb = Workbooks.Open(folderPath & fileName)
        wb.Password = password
        wb.Save
        wb.Close SaveChanges:=True
        fileName = Dir
    Loop
End Sub
 
Upvote 0
Solution
You can start with this. Test on copy.
VBA Code:
Sub PasswordProtectMultipleWorkbooks()
    Dim folderPath As String
    Dim password As String
    Dim fileName As String
    Dim wb As Workbook

    ' Set the folder path and password
    folderPath = "C:\path\to\your\directory\"
    password = "yourpassword"

    ' Check if the folder path ends with a backslash, if not add it
    If Right(folderPath, 1) <> "\" Then
        folderPath = folderPath & "\"
    End If

    fileName = Dir(folderPath & "*.xls*")
    Do While fileName <> ""
        Set wb = Workbooks.Open(folderPath & fileName)
        wb.Password = password
        wb.Save
        wb.Close SaveChanges:=True
        fileName = Dir
    Loop
End Sub
Thank you. This work. You have no idea how much time this will save me.
 
Upvote 0
Thank you. This work. You have no idea how much time this will save me.
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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