Macro to open all files in a folder

bookworm121

New Member
Joined
Jun 22, 2011
Messages
39
Is it possible to write a macro that would open all files in a certain folder? The folder name and location will always stay the same but the name of the files within and the amount of files may change.

The macro would open them all, and then copy the tabs from them and create a new spreadsheet with all the tabs from all the files in it.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
This code will open all workbooks in a folder all you have to do is add what you want to do inside the code. Adjust what I am doing in Red


Sub Open_My_Files()
Dim MyFile As String
MyPath = "M:\Access Files\"
MyFile = Dir(MyPath)
Do While MyFile <> ""
If MyFile Like "*.xls" Then
Workbooks.Open MyPath & MyFile
Sheets(1).Select
Range("C3") = 6
ActiveWorkbook.Close True
End If
MyFile = Dir
Loop
End Sub
 
Upvote 0
This would open them - change the folder to suit

Code:
Sub OpenFiles()
Dim MyFolder As String
Dim MyFile As String
MyFolder = "C:\example"
MyFile = Dir(MyFolder & "\*.xls")
Do While MyFile <> ""
    Workbooks.Open Filename:=MyFolder & "\" & MyFile
Loop
End Sub
 
Upvote 0
Trevor - just a quick note of thanks on this! I had a situation where had over 200 files linked to a comparison sheet by an overly complicated HLOOKUP and they just wouldn't recalculate. I was resorting to opening all 200 files, then closing them again, just to get the recalculation to work (after swearing a lot and pressing various F9 combinations!).

Your macro took what was a 15 minute process and sorted it in 2 minutes. Thanks!!
 
Upvote 0
Nice to read MichaelB1 thank you for the comments....
 
Upvote 0

Forum statistics

Threads
1,216,115
Messages
6,128,923
Members
449,479
Latest member
nana abanyin

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