Subfolders

bromy2004

Board Regular
Joined
Feb 8, 2008
Messages
63
Hi,

I'm trying to get a macro to do several step to Each file within subfolders.
i.e. i select a folder path (C:\Test\) the macro will then open each file in there and save to a different location (C:\Final\)

but i can seem to get it to loop through more than one subfolder.
Excel Workbook
G
25C:\Test\Folder 1\Subfolder 1
26C:\Test\Folder 1\Subfolder 2
27C:\Test\Folder 1\Subfolder 3
28C:\Test\Folder 1\Subfolder 4
29C:\Test\Folder 1\Subfolder 5
30C:\Test\Folder 2\Subfolder 1
31C:\Test\Folder 2\Subfolder 2
32C:\Test\Folder 2\Subfolder 3
33C:\Test\Folder 2\Subfolder 4
34C:\Test\Folder 2\Subfolder 5
35C:\Test\Folder 3\Subfolder 1
36C:\Test\Folder 3\Subfolder 2
37C:\Test\Folder 3\Subfolder 3
38C:\Test\Folder 3\Subfolder 4
39C:\Test\Folder 3\Subfolder 5
40C:\Test\Folder 4\Subfolder 1
41C:\Test\Folder 4\Subfolder 2
42C:\Test\Folder 4\Subfolder 3
43C:\Test\Folder 4\Subfolder 4
44C:\Test\Folder 4\Subfolder 5
45C:\Test\Folder 5\Subfolder 1
46C:\Test\Folder 5\Subfolder 2
47C:\Test\Folder 5\Subfolder 3
48C:\Test\Folder 5\Subfolder 4
49C:\Test\Folder 5\Subfolder 5
Option


So i can loop it through "Test" and the "Folder*" folders
but i cant loop it through each of the subfolders.

there could be anywhere from 1 folder to 10 sub-levels

please help me.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Depending on the version of Excel you are using, this macro will identify all Excel workbooks in the primary folder you specify, and all its subfolders.

Modify the example primary folder path of "C:\Your\File\Path" with whatever your primary folder really is

and

substitute this code block which is only for demonstration purposes...
Code:
MsgBox "There were " & .FoundFiles.Count & " file(s) found in primary path" & vbCrLf & strPath, , "FYI"
For i = 1 To .FoundFiles.Count
MsgBox .FoundFiles(i)
Next i
...with whatever code you had in mind to save those files or do whatever you were going to do to them.


Code:
Sub FilePathSearch()
With Application.FileSearch
Dim strPath$, i%
strPath = "C:\Your\File\Path"
.NewSearch
.LookIn = strPath
.SearchSubFolders = True
.Filename = "*.xls*"
.MatchTextExactly = True
If .Execute() > 0 Then

MsgBox "There were " & .FoundFiles.Count & " file(s) found in primary path" & vbCrLf & strPath, , "FYI"
For i = 1 To .FoundFiles.Count
MsgBox .FoundFiles(i)
Next i

Else
MsgBox "Nothing found in primary path" & vbCrLf & strPath
End If
End With
End Sub
 
Upvote 0
I'm getting an error on
Code:
With Application.FileSearch

"Run Time error 445"
"Object Doesn't Support this action"

I'm running Excel 2007
 
Upvote 0
That possibility was why I said the code depended on the version of Excel you are using because 2007 does not support FileSearch. I'm in front of the television watching Missouri vs Nebraska tonight, so I'll modify the code for 2007 and post it tonight.
 
Upvote 0
I currently have something similar to that.

using the
Code:
For Each SubFolder In Folder.SubFolders
however this only goes into 1 sub-folder.

there may be up to 10.

i have thought that maybe it could be a separate Macro
to search through a designated folder and run a second macro if there is a sub-folder.

but i haven't met with any success.:oops:
 
Upvote 0
Hello,

You want recursion - a procedure that recalls itself. Look at the code Tom linked to, closely.
 
Upvote 0

Forum statistics

Threads
1,215,493
Messages
6,125,131
Members
449,206
Latest member
burgsrus

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