View Single Post
  #4   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

Bernie,

First of all, I appreciate your help. You are correct that I will need to
be more specific and I think your code will work. However, my issue is going
to be that I just used the small number of files and folders as an example.
Within the results folder I will actually have somewhere in the neighborhood
of 200 files, and somewhere in the neighborhood of the same amount of
folders. So typing out the data to be included in myWords and mySearch will
be cumbersome to setup, and if any files or folders are added down the road I
will have to manually adjust that data list. This will be a process I have
to perform on a monthly basis so I would like to remove as much manual vba
adjustments on a on-going basis as possible.

Would there be a way to loop through the results folder to get the file
names and create an array of that information? And say all the other folders
are in a folder named "Everything", would there be a way to loop through all
subfolders within the "Everything" folder and make that an array as well?
That info would become myWords and mySearch if you will.



"Bernie Deitrick" wrote:

You need to specify how 'granular' you want to be: does the B in Blue count when their is a B in
ABC? Probably not....

So, perhaps use two lists of words to look for:

Sub TryNow()
Dim myWords As Variant
Dim myS As Variant
Dim mySearch As String

myWords = Array("Blue", "Green", "Red", "Yellow", "ABC", "DEF", "GHI")

mySearch = "Blue days, ABC nights"
For Each myS In myWords
If InStr(1, mySearch, myS) 0 Then
MsgBox """" & mySearch & """ has """ & myS & """ somewhere in it"
'Do something else, like copy the file
End If
Next myS

End Sub



HTH,
Bernie
MS Excel MVP


"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.