View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips[_6_] Bob Phillips[_6_] is offline
external usenet poster
 
Posts: 11,272
Default Perform code on all (closed) workbooks scattered across sub-folders of common parent folder

Dim oFSO

Sub LoopFolders()

Set oFSO = CreateObject("Scripting.FileSystemObject")

selectFiles "c:\MyTest"

Set oFSO = Nothing

End Sub


'---------------------------------------------------------------------------
Sub selectFiles(sPath)
'---------------------------------------------------------------------------
Dim Folder As Object
Dim Files As Object
Dim file As Object
Dim fldr

Set Folder = oFSO.GetFolder(sPath)

For Each fldr In Folder.Subfolders
selectFiles fldr.Path
Next fldr

For Each file In Folder.Files
If file.Type = "Microsoft Excel Worksheet" Then
Workbooks.Open Filename:=file.Path
.... your code here on Activeworkbook
Activeworkbook.Close
End If
Next file

End Sub


--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"ikr" wrote in message
...
I have some code that I want to be performed on all the closed workbooks

in
sub-folders of a common parent, viz:

parent/childfolder1/workbook1.xls
parent/childfolder2/workbook2.xls
parent/childfolder3/workbook3.xls
parent/childfolder3/workbook4.xls
parent/childfolder4/workbook5.xls
.
.
.
parent/childfolderX/workbookY.xls

These aren't the names of the folders and workbooks, I've just used them
here for illustration. Note that some of the childfolders contain more

than
one workbook on which I'd like to perform the code.

I've written the code, and it works fine, but at the moment I'm relying on
the user to run it on each workbook in turn. The code opens the workbook
chosen by the user via the Application.GetOpenFilename() method, performs
the code on that workbook, and then closes that workbook. The user then
moves onto the next workbook. Is there a way to get the code to run
automatically on each workbook in every sub-folder of the <parent folder
via one overall procedure? Basically, I want the user to just have to
specify the parent folder, and then VBA will do its stuff on all workbooks
in all childfolders beneath the parent? This sounds like it should be

easy,
but something tells me it's going to be very complicated!

TIA

Ian