+ Reply to Thread
Results 1 to 10 of 10

Copy files from one folder to another folder based on inputs from a table

Hybrid View

  1. #1
    Registered User
    Join Date
    03-27-2022
    Location
    Hong Kong
    MS-Off Ver
    Office2016
    Posts
    5

    Copy files from one folder to another folder based on inputs from a table

    Hi all,

    May I have your help to add below functions to the vba please?


    1. if source or destination folder do not exist, column D will remark" no source / destination file found".
    2. For other situations, when a file was copied successfully, the corresponding cell in column D will automatically remark " transferred", however, when copy failed, the corresponding cell in column D will automatically remark "failed"
    3. Where should I put on error resume next if I want the program to run through the last row?

    tab1.JPG



    ****************************************

    Dim fso
    Dim source_file As String
    Dim source_folder As String
    Dim destination_folder As String
    Dim lastrow As Integer

    lastrow = ActiveSheet.UsedRange.Rows.Count

    For i = 2 To lastrow
    source_file = Cells(i, "a")
    source_folder = Cells(i, "b")
    destination_folder = Cells(i, "c")

    Set fso = CreateObject("scripting.filesystemobject")
    fso.CopyFile (source_folder & source_file), destination_folder, True
    Cells(i, "d") = "successfully transferred"

    Next i

    End Sub
    *******************************************

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Copy files from one folder to another folder based on inputs from a table

    ....Wrong Thread....
    Last edited by Sintek; 10-09-2022 at 12:45 PM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Copy files from one folder to another folder based on inputs from a table

    Quote Originally Posted by sintek View Post
    ....Wrong Thread....
    Why Syntec? It's repeated?...

  4. #4
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Copy files from one folder to another folder based on inputs from a table

    Quote Originally Posted by beyond Excel View Post
    Why Syntec? It's repeated?...
    No...I posted solution for other thread in this one...Hence deletion...

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Copy files from one folder to another folder based on inputs from a table


    Quote Originally Posted by kentnood View Post
    1. if source or destination folder do not exist, column D will remark" no source / destination file found".
    Hi,

    why not better creating the destination folder if it does not exist ?
    Easy with the Windows function MakeSureDirectoryPathExists as you can see in threads within this forum ...

  6. #6
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Copy files from one folder to another folder based on inputs from a table

    Hello
    One way to do it:

    Sub Macro32()
    Dim C As Range, Tmp
    For Each C In Range("A2", Range("A1").End(xlDown))
      C(, 4).ClearContents
      Select Case True
        Case Dir(C(, 2), vbDirectory) = ""
          C(, 4) = "Nonexistent Source path"
        Case Dir(C(, 3), vbDirectory) = ""
          C(, 4) = "Nonexistent Destination path"
        Case Dir(C(, 2) & C) = ""
          C(, 4) = "Nonexistent File"
        Case Else
          FileCopy C(, 2) & C, C(, 3) & C
          C(, 4) = "Successfully transferred"
      End Select
    Next
    End Sub
    And comment if you change your mind and a new folder needs to be created.

  7. #7
    Registered User
    Join Date
    03-27-2022
    Location
    Hong Kong
    MS-Off Ver
    Office2016
    Posts
    5

    Re: Copy files from one folder to another folder based on inputs from a table

    Quote Originally Posted by beyond Excel View Post
    Hello
    One way to do it:

    Sub Macro32()
    Dim C As Range, Tmp
    For Each C In Range("A2", Range("A1").End(xlDown))
      C(, 4).ClearContents
      Select Case True
        Case Dir(C(, 2), vbDirectory) = ""
          C(, 4) = "Nonexistent Source path"
        Case Dir(C(, 3), vbDirectory) = ""
          C(, 4) = "Nonexistent Destination path"
        Case Dir(C(, 2) & C) = ""
          C(, 4) = "Nonexistent File"
        Case Else
          FileCopy C(, 2) & C, C(, 3) & C
          C(, 4) = "Successfully transferred"
      End Select
    Next
    End Sub
    And comment if you change your mind and a new folder needs to be created.
    Thank you very much, beyond Excel!

    This is what I wanted. And yes, I want to create the new destination folders too. Could you advise again, please?

  8. #8
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Copy files from one folder to another folder based on inputs from a table

    Quote Originally Posted by kentnood View Post
    Thank you very much, beyond Excel!

    This is what I wanted. And yes, I want to create the new destination folders too. Could you advise again, please?
    In the attached variant, if the destination path does not exist, the folder is created first, and then the file is copied:

    Sub Macro34()
    Dim C As Range, Tmp, mPath, iP
    For Each C In Range("A2", Range("A1").End(xlDown))
      If Right(C(, 2), 1) <> "\" Then C(, 2) = C(, 2) & "\"
      If Right(C(, 3), 1) <> "\" Then C(, 3) = C(, 3) & "\"
      C(, 4).ClearContents
      Select Case True
        Case Dir(C(, 2), vbDirectory) = ""
          C(, 4) = "Nonexistent Source path"
        Case Dir(C(, 2) & C) = ""
          C(, 4) = "Nonexistent File"
        Case Dir(C(, 3), vbDirectory) = ""
          Tmp = Split(C(, 3), "\"): mPath = ""
          For Each iP In Tmp
            If iP = "" Then Exit For
            mPath = mPath & iP & "\"
            If Dir(mPath, vbDirectory) = "" Then MkDir mPath
          Next
          FileCopy C(, 2) & C, C(, 3) & C
          C(, 4) = "Successfully transferred"
        Case Else
          FileCopy C(, 2) & C, C(, 3) & C
          C(, 4) = "Successfully transferred"
      End Select
    Next
    End Sub

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Copy files from one folder to another folder based on inputs from a table


    See post #3 ...

  10. #10
    Registered User
    Join Date
    03-27-2022
    Location
    Hong Kong
    MS-Off Ver
    Office2016
    Posts
    5

    Re: Copy files from one folder to another folder based on inputs from a table

    Thank you all for your input!

+ 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. [SOLVED] Macro to copy .xlsm files in folder and sub-folder to foldr C:\old pull files
    By Howardc1001 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-15-2021, 09:55 PM
  2. [SOLVED] Copy Folder then rename files in new folder from list in Excel
    By jimjones1958 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-20-2021, 05:00 PM
  3. VBA Copy specific files listed in Excel from folder & subfolders to another folder - FSO
    By HelloFriends in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-03-2019, 02:55 PM
  4. Copy Files from One Folder to Another Folder based on a List In Excel
    By civram1982 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-21-2019, 06:34 AM
  5. Replies: 18
    Last Post: 02-18-2019, 07:42 AM
  6. Replies: 12
    Last Post: 03-09-2015, 05:52 PM
  7. [SOLVED] VBA Code open files in folder, copy text to workbook-Next time folder opened copy only new
    By Bikeman in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-02-2013, 07:59 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