Using VBA Code (Excel 2007) To Open All Excel Files in a specific folder

mwmtech

New Member
Joined
Jul 5, 2009
Messages
29
Hi Guys, this code is causing me some difficulty. I am trying to use the code below in Excel 2007 to open all Excel files in a specified folder, but I am getting the run-time error '1004', any ideas on what I am doing wrong? Thanks in advance..

Sub FindOpenFiles()
Dim FSO As Scripting.FileSystemObject, folder As Scripting.folder, file As Scripting.file, wb As Workbook
Dim directory As String
directory = "C:\Users\Documents\VBA\VBA 2\Data"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set folder = FSO.GetFolder(directory)
Set wb = Workbooks.Open(directory)
For Each file In folder.Files
Workbooks.Open wb
Next file
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You don't actually appear to be opening the files that are specified with 'folder.Files'.

This appears to be trying to open a file with the name of the directory you want to look in.
Code:
Set wb = Workbooks.Open(directory)
I would recommend you just remove that line and within the loop try using file.
Code:
Sub FindOpenFiles()
Dim FSO As Scripting.FileSystemObject, folder As Scripting.folder, file As Scripting.file, wb As Workbook
Dim directory As String
 
    directory = "C:\Users\Documents\VBA\VBA 2\Data"
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set folder = FSO.GetFolder(directory)
    
    For Each file In folder.Files
        Workbooks.Open file
    Next file
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,935
Members
449,195
Latest member
Stevenciu

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