2016-02-26 144 views
0

我有一个在Excel中有50个文件路径的列表。如何在网络中搜索所有这些文件并将它们本地复制到我的文件夹? 如果路径不存在于网络中,我可以跳过这些吗? 谢谢。VBA,使用文件路径在本地复制和粘贴

EDIT ~~~

Sub Copy_Certain_Files_In_Folder() 
'This example copy all Excel files from FromPath to ToPath. 
'Note: If the files in ToPath already exist it will overwrite 
'existing files in this folder 
    Dim FSO As Object 
    Dim FromPath As String 
    Dim ToPath As String 
    Dim FileExt As String 

    FromPath = "I use network file here" '<< Change 
    ToPath = "local here" '<< Change 

    FileExt = "*.csv*" '<< Change 
    'You can use *.* for all files or *.doc for Word files 

    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 
+0

这对SO有多个答案。我首选的方法是使用scripting.filesystemobject的fileexist和其他函数查看这些关键字是否帮助您找到要查找的内容。 –

+0

@科迪G。感谢代码-y! – Jonathan

+0

只供参考http://www.rondebruin.nl/win/s3/win026.htm –

回答

0

假设你的文件路径是A列中的行上的片材名为mysheetname这应该工作1至50。因为这是在手机上输入的,请原谅我的错误。

Sub MyCopyFiles() 
    Dim FSO 
    Set FSO = CreateObject("Scripting.FileSystemObject") 
    Dim newpath as string: newpath ="C:\temp\" 
    For i=1 to 50 
    If FSO.fileexists(Sheets("mysheetname").Cells(i,1)) then 
     Call FSO.CopyFile(Sheets("mysheetname ").Cells(i,1),newpath+FSO.getFilename(Sheets("mysheetname").Cells(i,1))) 
    End if 
    Next i 

End sub 
+0

科迪,我在哪里把输出文件夹也移动文件?或者你只是解释这些代码的输入? – Jonathan

+0

用你想要的路径替换C:\ temp \。注意它需要以\ –

+0

为结尾我得到所需的对象'设置FSO = CreateObject(Scripting.FileSystemObject)'也将我的文件表更改为“mysheetname” – Jonathan