PDA

View Full Version : Open multiple password protected workbooks in a folder



rockybalboa
01-05-2017, 01:13 AM
Hi,
I have around 8 excel files in a folder of which some files (around 4) are password protected. I am looking for a way to open the password protected files via macro. I have the passwords for all of them.

Below is my code


Sub Run()
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog

'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

myPath = "D:\My\Team\"
If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
myExtension = "*.xlsm"

'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
Do While myFile <> ""

'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile) '(How to open some of the password protected workbooks?)

'Ensure Workbook has opened before moving on to next line of code
DoEvents

'Collate
wb.Sheets("Board").Select
Range("H8:L8").Select
Selection.Copy
ThisWorkbook.Activate
Sheets("Team").Select
Range("B1000").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues

'Close Workbook
wb.Close SaveChanges:=False

'Ensure Workbook has closed before moving on to next line of code
DoEvents

'Get next file name
myFile = Dir
Loop

'Message Box when tasks are completed
MsgBox "Task Complete!"

ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub


Thanks for your help.

GTO
01-05-2017, 03:26 AM
.... I have the passwords for all of them.

Is the password the same (universal) for all the workbooks?

rockybalboa
01-05-2017, 05:07 AM
No, it is different

GTO
01-05-2017, 06:26 AM
In that case, rather than using Dir() and a Do While...Loop, I would suggest listing the names of the workbooks in one column on a worksheet, the associated passwords in the next column, and use a For Each or For Next type loop. In gist, as you need to provide specific passwords for specific filenames, a Do...Loop would not seem easiest...

Mark

rockybalboa
01-05-2017, 11:35 PM
Thanks, will try that