[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
 
ehh...........tht possible...
if it happen, i think is best to avoid copy and set A File Error has Occurred? is the possible be able to do it?

Thanks mate~!

If you have, say 130, in Col A are you likely to have more than 1 file in the source folder that begins with 130?
 
Last edited:
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Give this a go
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 & "\" & Fname & "*")
                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
omg.. i just tried it.. it work like charm!

i notice i had to put in only the first few number or letter before _ and left out the filename extension and it copied~~

ya tht is what i want!

thanks Fluff, you so kind to help :)

Give this a go
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 & "\" & Fname & "*")
                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
Glad to help & thanks for the feedback
 
Upvote 0
Hi,

the previously script which is still usable for me. Is it possible to amend it to if File was not found in source folder, do no create folder?

Code:
Sub CopyFileWithNo[COLOR=#333333]Ext[/COLOR]()

    Dim Cl As Range
    Dim SrcFle As String
    Dim DestFle As String
    Dim Fname As String
    
    
    For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
        If Dir(Cl.Offset(, 2).Value, vbDirectory) = "" Then MkDir Cl.Offset(, 2).Value
        Fname = Cl.Value
        SrcFle = ""
        DestFle = ""
        SrcFle = Dir(Cl.Offset(, 1).Value & "" & Fname)
        DestFle = Dir(Cl.Offset(, 2).Value & "" & Fname)
        If Len(SrcFle) > 0 And Len(DestFle) = 0 Then
            FileCopy Cl.Offset(, 1).Value & "" & Fname, _
                Cl.Offset(, 2).Value & "" & Fname
            Cl.Offset(, 3).Value = "Copied"                     'File was copied to new folder
        ElseIf Len(SrcFle) = 0 Then
            Cl.Offset(, 3).Value = "File not found"             'File was not found in source folder
        ElseIf Len(DestFle) > 0 Then
            Cl.Offset(, 3).Value = "File Exists"                'File already exists in destination folder
        End If
    Next Cl
    
End Sub

Glad to help & thanks for the feedback
 
Last edited:
Upvote 0
The code I supplied in post#19 & post#22 does that
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
Hi,
I need some help on this code..

The filecopy working well BUT i had one issue.

if there are file which look like this

859_1_4_58111.pdf <-- file copied
859A_1_4_58111.pdf <-- More than 1 file found

it will say more than 1 file found but both file are belong to diff filename.
as anything after _ will be ignore..

i put 859 & 859A as my filename



Glad to help & thanks for the feedback
 
Last edited:
Upvote 0
Sorry, make a mistake by tell u to put ignore if
But i tot 859A is a new name? :/


Possible just follow COL A filename

as anything after _ will be ignore

If you have, say 130, in Col A are you likely to have more than 1 file in the source folder that begins with 130?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,283
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