VBA,使用文件路径进行本地复制和粘贴

时间:2016-02-26 18:52:20

标签: vba excel-vba excel

我在excel中有50个文件路径列表。如何在网络中搜索所有这些文件并将其本地复制到我的文件夹中? 如果网络中不存在路径,我可以跳过这些路径吗? 感谢。

编辑~~~

  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

1 个答案:

答案 0 :(得分:0)

假设您的文件路径位于名为mysheetname的工作表上的第A行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