VBA to open all Excel files in a specified folder

henry fam

New Member
Joined
Nov 25, 2013
Messages
15
Hi:
I want to open all Excel files in a specified folder and have the following code, but it didn't work:

Sub OpenFiles()
Dim MyFolder As String
Dim MyFile As String
MyFolder = "\\G:\Functional Teams\Finance\2013\Consolidation & Reporting\1013\Financial Package"
MyFile = Dir(MyFolder & "\*.xls")
Do While MyFile <> ""
Workbooks.Open Filename:=MyFolder & "\" & MyFile
MyFile = Dir
Loop
End Sub

It gave me a message Run-time error'52' "Bad file name or number", and the error was highlighted in the line MyFile = Dir(MyFolder & "\*.xls")
Why was it? How should I correct it?
Any help is very appreciated. Or please advise me other codes that work.
I'm using Excel 2010.
Thanks
Henry
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Not sure. Same idea works for me. I'm hoping you haven't put 1013 instead of 2013 in the MyFolder string ?
 
Upvote 0
Hi Lexxie:
I changed to a newa path, and it did work. My goodness. Thank you so much for your confirmation.
By the way, can you help me with another thing:
I want to add another macro to this vba code in order to change the layout of these reports including,
unprotect sheet,
Print Titles: portrait; margin: 0.25 for each side; center on page : horizontally.
Please help.
Thanks so much
henry
 
Upvote 0
I found this macro to be very useful, but is there any way to remove the predefined folder location and replace it with a prompt to allow the user to specify the location? I'd like to be able to share this with other people who are not familiar with the developer functions of excel. Thanks.
 
Upvote 0

Forum statistics

Threads
1,214,846
Messages
6,121,905
Members
449,054
Latest member
luca142

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