VBA to match list of files in windows folder and copy all to another folder

fotodj

New Member
Joined
Jul 19, 2014
Messages
27
With the code below I can find file named Sample1.jpg in folder located in "C:\Source\" and copy it to new location "D:\Job\".
How can I make a loop to copy all the files listed in Sheet1 in Colum G


VBA Code:
'In this Example I am Copying the File From "C:Temp" Folder to "D:Job" Folder
Sub MatchCopyAFile()

'Declare Variables
Dim FSO
Dim sFile As String
Dim sSFolder As String
Dim sDFolder As String

'This is Your File Name which you want to Copy
sFile = "Sample1.jpg"

'Change to match the source folder path
sSFolder = "C:\Source\"

'Change to match the destination folder path
sDFolder = "D:\Job\"

'Create Object
Set FSO = CreateObject("Scripting.FileSystemObject")

'Checking If File Is Located in the Source Folder
If Not FSO.FileExists(sSFolder & sFile) Then
    MsgBox "Specified File Not Found", vbInformation, "Not Found"
   
'Copying If the Same File is Not Located in the Destination Folder
ElseIf Not FSO.FileExists(sDFolder & sFile) Then
    FSO.CopyFile (sSFolder & sFile), sDFolder, True
    MsgBox "Specified File Copied Successfully", vbInformation, "Done!"
Else
    MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"
End If

End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Add these few lines of code to your macro. I'm assuming that the file names in column G are complete of estension and that in row 1 you have a header (adjust macro if necessary).
VBA Code:
Option Explicit
'In this Example I am Copying the File From "C:Temp" Folder to "D:Job" Folder
Sub MatchCopyAFile()
    'Declare Variables
    Dim FSO
    Dim sFile  As String
    Dim sSFolder As String
    Dim sDFolder As String
    Dim LR     As Long                            'last row column G   '<- added
    Dim x      As Long                            'generic counter for loop    '<- added
    'Change to match the source folder path
    sSFolder = "C:\Source\"
    'Change to match the destination folder path
    sDFolder = "D:\Job\"
    'Create Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    LR = Range("G" & Rows.Count).End(xlUp).Row    '<- added
    For x = 2 To LR                               '<- added (starts from row 2 because header in row 1)
        'This is Your File Name which you want to Copy
        sFile = Cells(x, "G")                     '<- changed
        'Checking If File Is Located in the Source Folder
        If Not FSO.FileExists(sSFolder & sFile) Then
            MsgBox "Specified File Not Found", vbInformation, "Not Found"
            'Copying If the Same File is Not Located in the Destination Folder
        ElseIf Not FSO.FileExists(sDFolder & sFile) Then
            FSO.CopyFile (sSFolder & sFile), sDFolder, True
            MsgBox "Specified File Copied Successfully", vbInformation, "Done!"
        Else
            MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"
        End If
    Next x                                        '<- added
    MsgBox "Done!"                                '<- added
End Sub
 
Upvote 0
Solution
Thanks for the positive feedback(y), glad having been of some help.
 
Upvote 0

Forum statistics

Threads
1,221,526
Messages
6,160,340
Members
451,637
Latest member
hvp2262

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