Using VBA to open Password Protected Files

bajwali

New Member
Joined
May 12, 2020
Messages
13
Office Version
  1. 2019
Platform
  1. Windows
Hi very new to VBA and coding so all help would be very appreciated.

So i have about 37 files which i need to open with VBA and then take some data and aggregate it into a summary however some of the files are password protected thankfully with the same password.

I am using the Application.Get Open file name to get the location of all the files and then usings a loop from 1 to n to open the FileNames(i) what can i do so that as the workbook is opened and it asks for the password it types in the password "password1".

Sub Aggregation_Data()
Dim FileNames As Variant, i As Integer, j As Integer
Dim TWB As Workbook, aWB As Workbook



Set TWB = ThisWorkbook

MsgBox ("Enter Files that you wish to import data from")

FileNames = Application.GetOpenFilename(FileFilter:="Excel Filter (*.xlsx), *xlsx", Title:="Open File(s)", MultiSelect:=True)

For i = 1 To UBound(FileNames)

Workbooks.Open FileNames(i)

Next i

End Sub
 
Hi

So i found the solution so sharing for anyone who finds the same issue in the future. It turns out it was the issue with a Comma in the wrong place.

VBA Code:
Sub Aggregation_Data()
Dim FileNames As Variant, i As Integer, j As Integer
Dim TWB As Workbook, aWB As Workbook



Set TWB = ThisWorkbook

MsgBox ("Enter Files that you wish to import data from")

FileNames = Application.GetOpenFilename(FileFilter:="Excel Filter (*.xlsx), *xlsx", Title:="Open File(s)", MultiSelect:=True)

For i = 1 To UBound(FileNames)

[B]Workbooks.Open FileNames(i), Password:="Password"[/B]

Next i

End Sub
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,215,061
Messages
6,122,922
Members
449,094
Latest member
teemeren

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