[VB help needed] Copy mulit folder files to Destination based on excel

harky

Active Member
Joined
Apr 8, 2010
Messages
405
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
column Acolumn Bcolumn C
Row 1File NameSource PathDestination Path (Copy)
Row 270401.PDFC:\Users\xxx\Desktop\MasterPath\Route 13C:\Users\xxx\Desktop\Saved Folder\70401
Row 370401.PDFC:\Users\xxx\Desktop\MasterPath\Route 16C:\Users\xxx\Desktop\Saved Folder\70401
Row 470401.PDFC:\Users\xxx\Desktop\MasterPath\Route 19C:\Users\xxx\Desktop\Saved Folder\70401
Row 570409.PDFC:\Users\xxx\Desktop\MasterPath\Route 16C:\Users\xxx\Desktop\Saved Folder\70409
Row 670409.PDFC:\Users\xxx\Desktop\MasterPath\Route 19C:\Users\xxx\Desktop\Saved Folder\70409

<tbody>
</tbody>

















Hi, i need some help as i had thousand of file to be move.
Is it possible to create marco and based on the excel list to COPY AND PASTE mulit folder files to Destination ?

For e.g i had 70401.PDF from Path C:\Users\xxx\Desktop\MasterPath\Route 13
COPY to C:\Users\xxx\Desktop\Saved Folder\70401
 
Simply add the _ to the file name in col A so that it looks like 859_ or 859A_
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Thanks!

somehow i notice mistakes at source path & destination folder

I had filename like.

From the data which i had on hand, i only able to list out the first part (the one in red).

859_1_5_11111.xxx
859A_1_4_22222.xxx
859_1_10_33333.xxx
859A_1_9_44444.xxx

The code only able to copy first 2 file, not the rest although they are copied from diff source path and had diff filename in whole.

Can the destination folder part be change? as long the fullname is not exist. File can be copied?


Md6XcjA.jpg




Simply add the _ to the file name in col A so that it looks like 859_ or 859A_
 
Last edited:
Upvote 0
How about
Code:
Sub CopyFile()

    Dim Cl As Range
    Dim SrcFle As String
    Dim DestFle As String
    Dim Fname As String
    Dim Cnt As Long
    
    
    For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
        Fname = Cl.Value
        SrcFle = ""
        DestFle = ""
        SrcFle = Dir(Cl.Offset(, 1).Value & "\" & Fname & "*")
        If Len(SrcFle) > 0 Then
            Cnt = 0
            Do While Len(SrcFle) > 0
                Cnt = Cnt + 1
                SrcFle = Dir
            Loop
            If Cnt = 1 Then
                SrcFle = Dir(Cl.Offset(, 1).Value & "\" & Fname & "*")
                If Dir(Cl.Offset(, 2).Value, vbDirectory) = "" Then MkDir Cl.Offset(, 2).Value
                DestFle = Dir(Cl.Offset(, 2).Value & "\" & SrcFle & "*")
                If Len(DestFle) = 0 Then
                    FileCopy Cl.Offset(, 1).Value & "\" & SrcFle, _
                        Cl.Offset(, 2).Value & "\" & SrcFle
                    Cl.Offset(, 3).Value = "Copied"                     'File was copied to new folder
                ElseIf Len(DestFle) > 0 Then
                    Cl.Offset(, 3).Value = "File Exists"                'File already exists in destination folder
                End If
            Else
                Cl.Offset(, 3).Value = "More than 1 file found"             'File was not found in source folder
            End If
        Else
            Cl.Offset(, 3).Value = "Source file doesn't exist"
        End If
    Next Cl
    
End Sub
 
Upvote 0
Thanks! Working now....

How about
Code:
Sub CopyFile()

    Dim Cl As Range
    Dim SrcFle As String
    Dim DestFle As String
    Dim Fname As String
    Dim Cnt As Long
    
    
    For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
        Fname = Cl.Value
        SrcFle = ""
        DestFle = ""
        SrcFle = Dir(Cl.Offset(, 1).Value & "\" & Fname & "*")
        If Len(SrcFle) > 0 Then
            Cnt = 0
            Do While Len(SrcFle) > 0
                Cnt = Cnt + 1
                SrcFle = Dir
            Loop
            If Cnt = 1 Then
                SrcFle = Dir(Cl.Offset(, 1).Value & "\" & Fname & "*")
                If Dir(Cl.Offset(, 2).Value, vbDirectory) = "" Then MkDir Cl.Offset(, 2).Value
                DestFle = Dir(Cl.Offset(, 2).Value & "\" & SrcFle & "*")
                If Len(DestFle) = 0 Then
                    FileCopy Cl.Offset(, 1).Value & "\" & SrcFle, _
                        Cl.Offset(, 2).Value & "\" & SrcFle
                    Cl.Offset(, 3).Value = "Copied"                     'File was copied to new folder
                ElseIf Len(DestFle) > 0 Then
                    Cl.Offset(, 3).Value = "File Exists"                'File already exists in destination folder
                End If
            Else
                Cl.Offset(, 3).Value = "More than 1 file found"             'File was not found in source folder
            End If
        Else
            Cl.Offset(, 3).Value = "Source file doesn't exist"
        End If
    Next Cl
    
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,243
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top