+ Reply to Thread
Results 1 to 8 of 8

Looping File Movement From a Number of Folders to One Folder

  1. #1
    Registered User
    Join Date
    06-08-2012
    Location
    isnorte daere
    MS-Off Ver
    Excel 2010
    Posts
    49

    Looping File Movement From a Number of Folders to One Folder

    Good afternoon,

    I have a script that moves all the files (.xlsx format) located in one folder to another folder. I eventually want to have code that moves all the files from 40 different folders into 1 folder. Basically, I have 40 or so folders that continually have new files put into them, and I want all of these files to be moved, through a macro, into 1 file. Below, I've posted the VBA code that I'm currently using to move files from one folder to another folder. Is there a way that I can loop the below script so that it repeats the process for a list of folders?

    Please Login or Register  to view this content.
    The above script moves every file in "UserAnalyticsReports2" to the folder "MasterUserAnalytics". Can I loop this code so that it repeats this process for a number of folders? Or will I have just repaste the above code for each individual folder?
    Last edited by jablo1312; 05-15-2014 at 09:39 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Looping File Movement From a Number of Folders to One Folder

    Hello jablo1312,

    By changing the macro to take the "from" and "to" folders as arguments, you can then use this in a loop.

    Please Login or Register  to view this content.
    Here is macro that reads the "from" folders from range "A1" down to the last entry in column "A" of the active worksheet.
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    06-08-2012
    Location
    isnorte daere
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Looping File Movement From a Number of Folders to One Folder

    Hey Leith, thanks for responding! So I'm trying to understand how exactly I would use the "to" and "from" functions as arguments in a loop. I want the "To" folder to be the same folder that receives the contents of a large number of "from" folders. Would I need to have put the list of folders into the 2nd string of code you have written above in order to have the first string of code loop through all of them?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Looping File Movement From a Number of Folders to One Folder

    Hello jablo1312,

    Technically, you don't have to have 2 arguments. You could simply pass the "From Folder" into the Sub and leave the "To Folder" hard coded.

    Supplying both makes the Sub more flexible. Later on, if you encounter a similar situation then you can use the same Sub with little or no modification.

    To understand and see how this is done, review the example macro Sub MoveFolders() in the previous post.

  5. #5
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Looping File Movement From a Number of Folders to One Folder

    Hi..

    If your list of Folder paths is in Column A (starting in Row 2).. this will do what you want I think..

    Change the strDest value to suit..

    Please Login or Register  to view this content.
    I am not sure why.. but at first I thought you wanted ALL files from ALL Subfolders from a SourceFolder moved to a Destination Folder..
    So I'll post the code i made initially to do that also..

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    06-08-2012
    Location
    isnorte daere
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Looping File Movement From a Number of Folders to One Folder

    Apo,

    Do I need to put the list of folders into an Excel document? Or is there a way to list them in the VBA code and use something like the call function to cycle through them?

  7. #7
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Looping File Movement From a Number of Folders to One Folder

    You could put them into an Array and loop through that array if you don't want to use sheet values..

  8. #8
    Registered User
    Join Date
    06-08-2012
    Location
    isnorte daere
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Looping File Movement From a Number of Folders to One Folder

    Hey guys, I'm sorry but I guess I'm not really following along. I've been playing with the codes for the last few hours and haven't been able to put them into a workable order. The above code in my initial post moves all files from the folder "UserAnalyticsReports2" to the folder "MasterUserAnalytics". I've been trying to use Leith's suggestion of making the "Frompath" an argument so that it can be amended to refer to a variety of folders. Maybe this isn't the optimal way, but I'd like to combine the initial code with a loop function and a list of folder paths in VBA so that the code loops through each folder in the list, moving the files in each folder to the hard-coded "ToPath" folder, which in this case is "MasterUserAnalytics". Sorry if this is already clear, its all a little above my head at the moment! Appreciate all the help so far.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Copy Files from one folder to different folders based on pdf file name
    By jtammyg in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-23-2014, 11:49 AM
  2. VBA code to open folders and pdf file inside the folders
    By kirtesh250187 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-26-2013, 03:17 AM
  3. Improvement - Option To Nest Folders as Sub Folder Under a Main Folder / Heading
    By :) Sixthsense :) in forum Suggestions for Improvement
    Replies: 4
    Last Post: 01-28-2013, 07:09 AM
  4. Looping a macro over all files in a folder for a file conversion
    By hippie_dream in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-10-2011, 12:11 PM
  5. Replies: 2
    Last Post: 05-07-2006, 03:20 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1