Help with this code..

PATSYS

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

I have the code below that copies certain files whose filenames are specified in Sheet1 Range A1:A1000 from C:\Test_Source\ to C:\Test_Destination\.

Problem is that when it finds a filename in Range A1:A1000 that does not exist in C:\Test_Source\, it gives debug error.

How can I make it in such a way that if it does not find the filename, it just ignore it and move on to the next filename?

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:\Test_Source\"
    ToPath = "C:\Test_Destination\"

    FileExt = "*.pdf"

    Set FSO = CreateObject("scripting.filesystemobject")
   
    x = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    For a = 1 To x
    b = Sheets("Sheet1").Cells(a, 1).Value
    
    [highlight]FSO.CopyFile Source:=FromPath & b & FileExt, Destination:=ToPath[/highlight]
    
    Next a
    
    MsgBox "You can find the files from " & FromPath & " in " & ToPath

End Sub

Thanks
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Just include another line before the FSO command saying

On Error Resume Next

e.g below.
Code:
    On Error Resume Next
    FSO.CopyFile Source:=FromPath & b & FileExt, Destination:=ToPath
 
Upvote 0
Just include another line before the FSO command saying

On Error Resume Next

e.g below.
Code:
    On Error Resume Next
    FSO.CopyFile Source:=FromPath & b & FileExt, Destination:=ToPath

Fantastic Nirvana, you saved my day. Thanks!

I knew the solution is simple (not for a vba dummy like me though).
 
Upvote 0

Forum statistics

Threads
1,214,989
Messages
6,122,622
Members
449,093
Latest member
catterz66

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