2016-08-03 4 views
2

私は2列ABのファイルのリストを持っています。VBAコピーファイルそれは存在しません

  • A列がソースでB
  • B列は、宛先

ソースから宛先へのコピー・ファイル以下のコードです。しかし、目的地が存在する場合、それは私にエラーを与える。それはそれが存在することを見つけるなら、それはanytingをしないように条件は何ですか?

コードに何が間違っていますか?

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

'iRow'何ですのような先のパスに食料調達するファイルやフォルダ

  • の存在を確認するために、1つの共通の機能を使用していますか? '目的地 'とは何ですか? –

  • +0

    @SiddharthRout私はコードを編集します – JeanLo

    +0

    最後の質問が1つあります。あなたはcol AとCol Bの価値は何ですか?ここに入力してください。 –

    答えて

    1
    If Not fso.FileExists(ClientsFolderDestination) Then 
        fso.CopyFile source, ClientsFolderDestination 
    End If 
    

    か、する場合はを上書き先ファイル

    fso.CopyFile source, ClientsFolderDestination, True 
    

    CopyFile Method

    2

    はこれを試してみてください。

    1. これはMicrosoft Scripting Runtime Libraryを使用しません。
    2. それは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 
    
    関連する問題