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?
Thanks
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