+ Reply to Thread
Results 1 to 7 of 7

Use Excel VBA to Copy multiple files to different folders

  1. #1
    Registered User
    Join Date
    03-23-2014
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    14

    Use Excel VBA to Copy multiple files to different folders

    I need to back up files, which may be excel or MSword, by copying them to the backup directory, say X:\backup\. Is there any vba can help to backup the files? I expect the vba can copy all file listed in column A, e.g. C:\test1\File1.xls or C:\test2\File2.doc, and then pasted to the directory in column B. e.g. X:\backup\Folder1\ or X:\backup\Folder2\. Thanks for help.

  2. #2
    Valued Forum Contributor ranman256's Avatar
    Join Date
    07-29-2012
    Location
    Kentucky
    MS-Off Ver
    Excel 2003
    Posts
    1,177

    Re: Use Excel VBA to Copy multiple files to different folders

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    03-23-2014
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Use Excel VBA to Copy multiple files to different folders

    I have found codes from the web and it suit my requirement that all files listed in column B will be copied to the directory listed in column C. However, when any files listed in the cell of column B cannot be found, there will be error. I hope that if anyone can help to add the error handling in the macro that if the file listed in column C cannot be found, it will skip the copy of this file and excute the file in next cell and also show a message, e.g. Fail to copy, in the same row in column D. The codes are quoted as below. Thanks for help.

    Sub Copy_Files()
    Dim cell As Range

    For Each cell In Range("B2", Range("B" & Rows.Count).End(xlUp))
    FileCopy Source:=cell.Value, Destination:=cell.Offset(, 1).Value
    Next cell

    End Sub

  4. #4
    Registered User
    Join Date
    03-23-2014
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Use Excel VBA to Copy multiple files to different folders

    I modified the copy so that it can skip the copy and show "Fail" if the source file cannot be found. The updated codes are as below. Thank all for help.

    Sub Copy_Files()
    Dim cell As Range


    For Each cell In Range("B2", Range("B" & Rows.Count).End(xlUp))

    File = Dir(cell.Value)
    If Len(File) > 0 Then
    FileCopy Source:=cell.Value, Destination:=cell.Offset(, 1).Value
    Else
    cell.Offset(, 2).Value = "Fail"
    End If
    Next cell


    Range("A1").Select

    End Sub

  5. #5
    Registered User
    Join Date
    06-03-2012
    Location
    malaysia
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Use Excel VBA to Copy multiple files to different folders

    Hi mm1234mail,

    Good evening.
    Do you mind to share your .xlsm file here? It doesnt work for me based on your code given.

    Thank you
    YS

  6. #6
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,448

    Re: Use Excel VBA to Copy multiple files to different folders

    As this thread is 6 years old I suggest you start a new thread with all your requirements, code, sheet,etc..

  7. #7
    Registered User
    Join Date
    06-03-2012
    Location
    malaysia
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Use Excel VBA to Copy multiple files to different folders

    Noted with many thank

+ 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. Use Excel VBA to Copy multiple files from different source folders to different folders
    By mm1234mail in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-12-2014, 01:17 PM
  2. How to copy and rename files in vba by searching multiple folders for files
    By razorace in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-04-2014, 04:21 AM
  3. Replies: 0
    Last Post: 01-11-2013, 12:05 AM
  4. [SOLVED] Copy range from multiple files in multiple folders to single sheet in master WB
    By Royzer in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 07-18-2012, 03:40 PM
  5. Copy multiple files from folders
    By jeff p in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-30-2009, 02:27 AM

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