Question: I am working on a VBA Macro that will open multiple worksheets at a time per year with their respective passwords. For example, I have

I am working on a VBA Macro that will open multiple worksheets at a time per year with their respective passwords. For example, I have folders for years 2005-2011 that contain files, where some files have password protection, and it is a different password per file. I need to be able to open those files back to back without entering the passwords manually every time. Here is what I have so far:

Sub Magic()

Dim i As Long, wb As Workbook, bOpen As Boolean, fName As String, year As Integer On Error Resume Next 'turn error reporting off

Dim myArray(14 To 20) As String

myArray(14) = "lila" myArray(15) = "boss" myArray(16) = "slip" myArray(17) = "pogo" myArray(18) = "duck" myArray(19) = "wash" myArray(20) = "guru"

year = 2005

Do While year < 2012

directory = "I:\ICRAS\Papers & PKIS\" & year & "\PKI" & year & "\" fName = Dir(directory & "*.xl??") For i = LBound(myArray) To UBound(myArray) Set wb = Workbooks.Open(fName, WriteResPassword:=myArray(i)) If Not wb Is Nothing Then bOpen = True: Exit For Next i On Error GoTo 0 'turn error reporting back on

If bOpen Then MsgBox "File was opened with password: " & myArray(i) Else MsgBox "Password not found for file!" End If

Loop End Sub

I keep getting syntax errors and I think it has to do with the Workbook.Open part. I appreciate the help.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!