VBA copyfile它不需要';不存在

VBA copyfile它不需要';不存在,vba,excel,Vba,Excel,我有一个文件列表,分为两列a和B A列是B的源 B列是目的地 下面的代码将文件从源复制到目标。但是如果目的地存在,它会给我错误。什么是条件,如果它发现它存在,它将不会做任何事情 代码有什么问题 Sub FC_Copy() Dim ClientsFolderDestination Dim fso As New FileSystemObject Dim rep_destination Dim source lastrow = ThisWorkbook.Worksheets("XC

我有一个文件列表,分为两列
a
B

  • A
    列是B的源
  • B
    列是目的地
下面的代码将文件从源复制到目标。但是如果目的地存在,它会给我错误。什么是条件,如果它发现它存在,它将不会做任何事情

代码有什么问题

  Sub FC_Copy()

Dim ClientsFolderDestination
Dim fso As New FileSystemObject
Dim rep_destination
Dim source

    lastrow = ThisWorkbook.Worksheets("XClients").Cells(Application.Rows.Count, 1).End(xlUp).Row

    For i = 5 To lastrow
        source = ThisWorkbook.Worksheets("XClients").Cells(i, 1).Value
        ClientsFolderDestination= ThisWorkbook.Worksheets("XClients").Cells(i, 2).Value
        If fso.FileExists(source) Then
            rep_destination = Left(ClientsFolderDestination, Len(ClientsFolderDestination) - Len(fso.GetFileName(ClientsFolderDestination)) - 1)

         If Not fso.FolderExists(rep_destination) Then
          sub_rep = Split(rep_destination, "\")
          myrep = sub_rep(0)
          If Not fso.FolderExists(myrep) Then
              MkDir myrep
           End If
           For irep = 1 To UBound(sub_rep)
              myrep = myrep & "\" & sub_rep(irep)
               If Not fso.FolderExists(myrep) Then
                    MkDir myrep
               End If
         Next
    End If

            fso.CopyFile source, ClientsFolderDestination
        End If
    Next i
end sub
或者,如果要覆盖目标文件

fso.CopyFile source, ClientsFolderDestination, True
试试这个

  • 这不使用
    Microsoft脚本运行时库
  • 它使用一个公共函数来检查文件和文件夹是否存在
  • 它满足像
    C:\Sample.xlsx
  • 代码

    Sub FC_Copy()
        Dim ws As Worksheet
        Dim source As String, Destination As String, sTemp As String
        Dim lRow As Long, i As Long, j As Long
        Dim MyAr As Variant
    
        Set ws = ThisWorkbook.Sheets("XClients")
    
        With ws
            '~~> Find Last Row
            lRow = .Range("A" & .Rows.Count).End(xlUp).Row
    
            For i = 5 To lRow            
                source = .Range("A" & i).Value
                Destination = .Range("B" & i).Value                
                MyAr = Split(Destination, "\")
    
                '~~> This check is required for destination paths like C:\Sample.xlsx
                If UBound(MyAr) > 1 Then
                    sTemp = MyAr(0)                
                    For j = 1 To UBound(MyAr)
                        sTemp = sTemp & "\" & MyAr(j)
                        If Not FileFolderExists(sTemp) = True Then MkDir sTemp
                    Next j
                End If
    
                If Not FileFolderExists(Destination) Then FileCopy source, Destination
            Next i
        End With
    End Sub
    
    Public Function FileFolderExists(strFullPath As String) As Boolean
        On Error GoTo Whoa
        If Not Dir(strFullPath, vbDirectory) = vbNullString Then FileFolderExists = True
        On Error GoTo 0
    Whoa:
    End Function
    

    什么是iRow?什么是目的地?@SiddharthRout我编辑代码最后一个问题,A列和B列的值是多少?在这里输入。有一种更简单的方法c:\Mycomputer\Folder\client1.xlsx,目标是:c:\Mycomputer\folder2\client1.xlsx我已经发布了答案。看看这是否对你有帮助。您可能需要刷新页面
    Sub FC_Copy()
        Dim ws As Worksheet
        Dim source As String, Destination As String, sTemp As String
        Dim lRow As Long, i As Long, j As Long
        Dim MyAr As Variant
    
        Set ws = ThisWorkbook.Sheets("XClients")
    
        With ws
            '~~> Find Last Row
            lRow = .Range("A" & .Rows.Count).End(xlUp).Row
    
            For i = 5 To lRow            
                source = .Range("A" & i).Value
                Destination = .Range("B" & i).Value                
                MyAr = Split(Destination, "\")
    
                '~~> This check is required for destination paths like C:\Sample.xlsx
                If UBound(MyAr) > 1 Then
                    sTemp = MyAr(0)                
                    For j = 1 To UBound(MyAr)
                        sTemp = sTemp & "\" & MyAr(j)
                        If Not FileFolderExists(sTemp) = True Then MkDir sTemp
                    Next j
                End If
    
                If Not FileFolderExists(Destination) Then FileCopy source, Destination
            Next i
        End With
    End Sub
    
    Public Function FileFolderExists(strFullPath As String) As Boolean
        On Error GoTo Whoa
        If Not Dir(strFullPath, vbDirectory) = vbNullString Then FileFolderExists = True
        On Error GoTo 0
    Whoa:
    End Function