View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
hurlbut777 hurlbut777 is offline
external usenet poster
 
Posts: 32
Default VBA Macro - Loop through folder and move files to other folder

Ronald,

Thank you for your response. If I am reading your code correctly it should
automate the excel file piece I need but I would have to build hundreds of If
statements specificly listing out each folder...if possible I would like to
avoid that but it is beginnning to look like I may just have to bite the
bullet.

"Ronald R. Dodge, Jr." wrote:

You can use something like the following code:

Public Sub pcdSaveFile()
Dim l_objFileSystemObject as Scripting.FileSystemObject,
l_objSourceFolder As Scripting.Folder
Dim l_objFiles as Scripting.Files, l_objCurrentFile as Scripting.File
Set l_objFileSystemObject = New Scripting.FileSystemObject
Set l_objSourceFolder =
l_objFileSystemObject.GetFolder("O:\ExcelFiles\Res ultsFolder")
Set l_objFiles = l_objSourceFolder.Files
For Each l_objCurrentFile in l_objFiles
If VBA.InStr(1, l_objCurrentFile.Name, "Red",vbTextCompare) 0 Then
l_objCurrentFile.Copy "O:\ExcelFiles\RedFolder\" &
l_objCurrentFile.Name, True
End If
Next
Set l_objFiles = Nothing
Set l_objSourceFolder = Nothing
Set l_objFileSystemObject = Nothing
end Sub

--
Thanks,

Ronald R. Dodge, Jr.
Production Statistician
Master MOUS 2000
"hurlbut777" wrote in message
...
I have 5 folders (ABC_Blue, ABC_Green, ABC_Red, ABC_Yellow, Results). The
folder named Results contains three files (Blue.xls, Green.xls, Red.xls).

I need to create a macro to loop through the results folder, and copy
excel
files into the other 4 folders based on whether or not any part of the
folder
name contains the name of the exel files. As an example, since ABC_Blue
folder contains Blue within its name, the Blue.xls file should be copied
to
this location.

I would be one happy camper if the above could be accomplished, but I
wouldn't know what to do with myself if in addition some functionality
could
be added to show any files within the results folder that were not copied
somewhere else. As an example, if there was an excel file name Purple.xls
within the Results folder, it wouldn't be copied to another folder because
there isn't one containing Purple within its name.

I know enough VBA to be dangerous, so if someone can help me get started
I'm
sure I can muddle through the rest eventually.