Open all files in all folders in a directory recursively

cheeseflea

New Member
Joined
Oct 19, 2006
Messages
5
I have a directory which includes a bunch of folders within folders within folders, etc. I am trying to write a macro that will go through every folder in the path, including all subfolders, and open every file with a specific file type one at a time. I have another macro I have written that makes changes to a file once it is opened, but I am having trouble getting all the files to open without hardcoding the specific path for each file(which is too time consuming and not realistic). I have tried a bunch of different things. I would even be happy if I could get a list of all the files in the entire path that meets the file type criteria. Thanks in advance.

cheeseflea
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

wongm003

Board Regular
Joined
Aug 8, 2005
Messages
237
You can use the application.filesearch and there is a parameter to search subfolders...

Code:
Public Sub my_find_files()

   'search for files
    With Application.FileSearch
        .NewSearch
        .LookIn = "C:\starting_dir"
        .SearchSubFolders = True
        .FileType = msoFileTypeExcelWorkbooks
        .Execute

        For x = 1 To .FoundFiles.Count
            'open workbook
            Workbooks.Open (.FoundFiles(x))
            
            'do what you want to the workbook
            Call your_macro
            
            'close workbook
            ActiveWorkbook.Close
        Next x
    End With

End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,133,459
Messages
5,658,893
Members
418,475
Latest member
ExcelBeginner233

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
Top