VBA,使用文件路径在本地复制和粘贴
问题描述:
我有一个在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
答
假设你的文件路径是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
这对SO有多个答案。我首选的方法是使用scripting.filesystemobject的fileexist和其他函数查看这些关键字是否帮助您找到要查找的内容。 –
@科迪G。感谢代码-y! – Jonathan
只供参考http://www.rondebruin.nl/win/s3/win026.htm –