+ Reply to Thread
Results 1 to 11 of 11

VBA Module to copy all files from a folder and it's sub-folders into one location

  1. #1
    Registered User
    Join Date
    09-06-2019
    Location
    Poland
    MS-Off Ver
    365
    Posts
    6

    Question VBA Module to copy all files from a folder and it's sub-folders into one location

    Hello everyone,

    I'm trying to get a macro working. It's sole purpose would be to copy all files from a folder and it's sub-folders into a single location (regardless the file type).

    Now I'm an absolute novice when it comes to any coding and I mainly use codes found on the internet.

    I managed to find something that suits me almost perfectly but in regard to sub-folders it copies them whole and I just want the files contained within to be copied.

    I tried my best to make it work on my own but I've reached the stage in my frustration when I want to ask for help.

    Please advise how to copy pnly the files contained in sub-folders.

    The element that I need modified would be:

    ------------------------------------
    Please Login or Register  to view this content.
    ------------------------------------



    WHOLE CODE:
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Last edited by alansidman; 09-09-2019 at 08:08 AM.

  2. #2
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    861

    Re: VBA Module to copy all files from a folder and it's sub-folders into one location

    Try replacing this part of the code:
    Please Login or Register  to view this content.
    With this one:

    Please Login or Register  to view this content.
    .. and don't forget to have fun!
    Bogdan.

    mark SOLVED and Add Reputation if my answer pleases you

  3. #3
    Registered User
    Join Date
    09-06-2019
    Location
    Poland
    MS-Off Ver
    365
    Posts
    6

    Re: VBA Module to copy all files from a folder and it's sub-folders into one location

    Works like a charm!
    It was the fastest and most helpful answer I ever got on the Internet.
    Thank you so much

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,873

    Re: VBA Module to copy all files from a folder and it's sub-folders into one location

    Code Tags Added
    Your post does not comply with Rule 2 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  5. #5
    Registered User
    Join Date
    09-06-2019
    Location
    Poland
    MS-Off Ver
    365
    Posts
    6

    Re: VBA Module to copy all files from a folder and it's sub-folders into one location

    alansidman
    Thank you for this.
    I will try my best to adhere to the forum's rules

    bulina2k
    I just noticed that the macro digs through the first level of subfodlers.
    Could it be modified to dig as far as the subfolders go?

  6. #6
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    861

    Re: VBA Module to copy all files from a folder and it's sub-folders into one location

    Hi there,

    Try to use this 2 subs instead of your original CopyAFolder sub:

    Please Login or Register  to view this content.
    I have to mention this is not tested! I will try it when I reach home cause now i'm in a hurry.

  7. #7
    Registered User
    Join Date
    09-06-2019
    Location
    Poland
    MS-Off Ver
    365
    Posts
    6

    Re: VBA Module to copy all files from a folder and it's sub-folders into one location

    Tried it, does not seem to work for now.
    I tried tweaking it but I'm not above admitting this is way beyond my level

  8. #8
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    861

    Re: VBA Module to copy all files from a folder and it's sub-folders into one location

    Ok, I'm sorry for the late reply, I just test the code successfully. What seems to be the problem? Do you receive an error ? What "does not seem to work for now" means? Please reply with more details. Thanks!

  9. #9
    Registered User
    Join Date
    09-06-2019
    Location
    Poland
    MS-Off Ver
    365
    Posts
    6

    Re: VBA Module to copy all files from a folder and it's sub-folders into one location

    Hello there

    I replaced the below with the provided code and it does copy all loose files from the Source folder's sub-folders. It does not, however, copy files from their subfolders (2nd level subfolders let's say). It also now omits Source's folder loose files. Below, you'll also find how I imagine the code to work.
    I appreciate your help with this. I fail miserably to make it work myself :D

    Attachment 641156

    Please Login or Register  to view this content.

  10. #10
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    861

    Re: VBA Module to copy all files from a folder and it's sub-folders into one location

    Ok, that attachment doesn't work, you'll have to "Go Advanced" and use "Manage Attachments" link for you to upload files.
    Me I've created some xlsx files in a multi-subfolder structure and executed this modules:
    (everything worked just fine)

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    09-06-2019
    Location
    Poland
    MS-Off Ver
    365
    Posts
    6

    Re: VBA Module to copy all files from a folder and it's sub-folders into one location

    I'm a dimwit.
    The first modified sub you've sent works perfectly of course.

    It's just that I needed to change "*.xlsx" to "*.*"
    My bad, your answer was perfect, thank you again!

+ 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 or cut pdf files from folders and subfolders into one single folder
    By druva in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-25-2019, 01:27 PM
  2. Prompt User Folder Location and Store as Directory for Creating Folders
    By kozman2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-06-2017, 04:58 PM
  3. Different Hyperlinks as source folders to copy all files in new folder
    By mullean3 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-20-2017, 09:55 AM
  4. Needs macro to copy excel files from folders, subfolders to new folder
    By genetist in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-08-2014, 08:53 AM
  5. create new folder at one location and copy photos in that folder from other location
    By meer_ali in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-22-2014, 05:20 AM
  6. 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
  7. Moving multiple folders and files to different location
    By slipit1 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 01-05-2009, 05:51 PM

Tags for this Thread

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