Copy only certain PDF files to folder

PATSYS

Well-known Member
Joined
Mar 12, 2006
Messages
1,750
Hi all,

I need a VBA code that copies a list of PDF files found in C:\Users\Ron\Data to C:\Users\Ron\Test. The list of PDF filenames to be copied can be found in Range A1:A100 of sheet1.

I found below code when searching the net. I think it is close to what I need with little tweaking...


Code:
Sub Copy_Certain_Files_In_Folder()

    Dim FSO As Object
    Dim FromPath As String
    Dim ToPath As String
    Dim FileExt As String

    FromPath = "C:\Users\Ron\Data" 
    ToPath = "C:\Users\Ron\Test"    

    FileExt = "*.xl*" 
    

    If Right(FromPath, 1) <> "\" Then
        FromPath = FromPath & "\"
    End If

    Set FSO = CreateObject("scripting.filesystemobject")

    If FSO.FolderExists(FromPath) = False Then
        MsgBox FromPath & " doesn't exist"
        Exit Sub
    End If

    If FSO.FolderExists(ToPath) = False Then
        MsgBox ToPath & " doesn't exist"
        Exit Sub
    End If

    FSO.CopyFile Source:=FromPath & FileExt, Destination:=ToPath
    MsgBox "You can find the files from " & FromPath & " in " & ToPath

End Sub

Thanks
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Here you go.

Code:
Sub CopyFilesToFolder()
    Dim FromPath As String
    Dim ToPath As String
    FromPath = "C:\Users\Ron\Data\"
    ToPath = "C:\Users\Ron\Test\"
    
    Set objFSO = CreateObject("scripting.filesystemobject")
    Set objfolder = objFSO.getfolder(FromPath)
    
    For Each MyFile In objfolder.Files
        If objFSO.GetExtensionName(MyFile) = "pdf" Then
            objFSO.CopyFile Source:=MyFile.Path, Destination:=ToPath
        End If
    Next
End Sub
 
Upvote 0
Sorry try this instead.

Code:
Sub CopyFilesToFolder()
    Dim FromPath As String
    Dim ToPath As String
    Dim i As Integer
    i = 1
    
    FromPath = "C:\temp\"
    ToPath = "C:\ztemp\"
    
    Set objFSO = CreateObject("scripting.filesystemobject")
    
    Do While Worksheets(1).Cells(i, 1) <> ""
        Set objFile = objFSO.GetFile(FromPath & Worksheets(1).Cells(i, 1).Value)
        If objFSO.GetExtensionName(objFile) = "pdf" Then
            objFile.Copy Destination:=ToPath
        End If
        i = i + 1
    Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,605
Members
449,089
Latest member
Motoracer88

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