+ Reply to Thread
Results 1 to 8 of 8

Copying folder and contents to new directory using VBA

  1. #1
    Registered User
    Join Date
    04-26-2012
    Location
    Liverpool
    MS-Off Ver
    Excel 2007
    Posts
    2

    Copying folder and contents to new directory using VBA

    I am new to the form and new to using macros or VBA in Excel and was hoping that someone could help with a problem. I have created a list of folders and their file paths in Column A of a spreadsheet and would like to copy the folders and their contents to a new location shown in Column B. eg

    g:\my documents\Folder 1 h:\Folder copy\Folder 1
    g:\my documents\Folder 2 h:\Folder copy\Folder 2
    g:\my documents\Folder 3 h:\Folder copy\Folder 3
    g:\my documents\Folder 4 h:\Folder copy\Folder 4
    g:\my documents\Folder 5 h:\Folder copy\Folder 5

    I would like to do this as a macro so that all the folders and their contents are copied to the new location and any folders in Column A that are not found are ignored. As there are over 5,000 folders to be copied I would like it to run automatically until all folders that are found in Column A are copied to the appropriate location in Column B.

    I would be most grateful if someone could assist me.

  2. #2
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Re: Copying folder and contents to new directory using VBA

    Here's a good place to start: http://www.rondebruin.nl/folder.htm

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

    Re: Copying folder and contents to new directory using VBA

    Hello Seymour100,

    Welcome to the Forum!

    You may find this routine does what you asked. Add a new module to your workbook's VBA project and paste the code below in it. You can then assign the macro to a button or run it manually. This macro assumes the folders in column "B" already exist.

    Please Login or Register  to view this content.
    Last edited by Leith Ross; 04-26-2012 at 04:13 PM.
    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!)

  4. #4
    Registered User
    Join Date
    04-26-2012
    Location
    Liverpool
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Copying folder and contents to new directory using VBA

    Jomili, thanks for your quick response and the link you provided. I am totally new to VBA and I can see how the routine on the other post will copy the folders. However, it seems to only work for one location to another and I need the macro to work its own way through the list (loop?), ignoring any folders it cannot find. Can you point me in the direction of how this could be achieved.

    Thanks so much for the help you have already provided.

  5. #5
    Registered User
    Join Date
    05-03-2012
    Location
    World
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Copying folder and contents to new directory using VBA

    Can somebody help me modify this example macro like following:

    I would look for the Project ID column B in excel from Target folder and move the DX-1000*.xls to the Destination.

    Then it would loop until no more rows has data

    Target Project ID Month Destination
    C:\Dataretrivalfolder DX-1000 April C:\Projectfolder\DX-1000\April
    C:\Dataretrivalfolder DX-1001 April C:\Projectfolder\DX-1001\April
    C:\Dataretrivalfolder DX-1002 April C:\Projectfolder\DX-1002\April
    C:\Dataretrivalfolder DX-1003 April C:\Projectfolder\DX-1003\April

    Many Thanks,

    EnzioL



    Sub Copy_Certain_Files_In_Folder()
    'This example copy all Excel files from FromPath to ToPath.
    'Note: If the files in ToPath already exist it will overwrite
    'existing files in this folder
    Dim FSO As Object
    Dim FromPath As String
    Dim ToPath As String
    Dim FileExt As String

    FromPath = "C:\Users\Ron\Data" '<< Change
    ToPath = "C:\Users\Ron\Test" '<< Change

    FileExt = "*.xl*" '<< Change
    'You can use *.* for all files or *.doc for word files

    If Right(FromPath, 1) <> "\" Then
    FromPath = FromPath & "\"
    End If

    Set FSO = CreateObject("scripting.filesystemobject")

    If FSO.FolderExists(FromPath) = False Then
    MsgBox FromPath & " doesn't exist"
    Exit Sub
    End If

    If FSO.FolderExists(ToPath) = False Then
    MsgBox ToPath & " doesn't exist"
    Exit Sub
    End If

    FSO.CopyFile Source:=FromPath & FileExt, Destination:=ToPath
    MsgBox "You can find the files from " & FromPath & " in " & ToPath

    End Sub

  6. #6
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Re: Copying folder and contents to new directory using VBA

    Seymour100,
    I'm so sorry I missed your last post! I've attached a workbook that I think will do what you want. It's based on the filepaths being in A and B, but I think you can easily change that to whatever suits you. The macro is shown below for anyone who wants it (it's just a minor derivation from Ron DeBruin's code).

    EnzioL,
    Please begin a new post of your own; it's considered poor form to hijack another thread (I know: I've done it before myself).

    Please Login or Register  to view this content.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    08-24-2017
    Location
    St Paul MN USA
    MS-Off Ver
    2010
    Posts
    1

    Re: Copying folder and contents to new directory using VBA

    This is great...does anyone know how to slightly change this to create the destination folder if it does not exist?

    Regards,

  8. #8
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,065

    Re: Copying folder and contents to new directory using VBA

    The basic code is already in the macro:
    Please Login or Register  to view this content.
    I know I'm not stupid, but I suspect I'm a lot stupider than I think I am

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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