从Excel宏从一个目标复制到另一个目标

时间:2015-08-13 09:32:32

标签: vba excel-vba excel

我遇到一个问题,即此宏将一个目标单元格的文档从一个源复制到另一个源。但是,当我想选择一系列细胞时,它不起作用。我错过了什么原则,当我有多个目的地时,我收到错误?你会在下面看到我的“to path”是一个给出错误的范围......

Sub xArray()

    Application.ScreenUpdating = False

    Dim fso As Object
    Dim FromPath As String
    Dim ToPath As String
    Dim Drive(1 To 2) As String
    Drive(1) = "O"
    Drive(2) = "P"

    For i = 1 To 2
        FromPath = "C:\Users\155555\Desktop\Source"
        ToPath = Range("A1:A2")

        If Right(FromPath, 1) = "\" Then
            FromPath = Left(FromPath, Len(FromPath) - 1)
        End If

        If Right(ToPath, 1) = "\" Then
            ToPath = Left(ToPath, Len(ToPath) - 1)
        End If

        Set fso = CreateObject("scripting.filesystemobject")

        If fso.FolderExists(FromPath) = False Then
            MsgBox FromPath & " doesn't exist"

            Exit Sub
        End If

        fso.CopyFolder Source:=FromPath, Destination:=ToPath

    Next i

End Sub

谢谢,

2 个答案:

答案 0 :(得分:0)

'目的地'必须是一个字符串。 如果你想用'i'循环,你可以尝试:

ToPath = Range("A" & i)

答案 1 :(得分:-1)

Sub Copy_Files()

    Dim cell As Range

    For Each cell In Range("E1", Range("E" & Rows.Count).End(xlUp))

        FileCopy Source:=cell.Value, Destination:=cell.Offset(, 1).Value

    Next cell

End Sub