满足条件时将文件(单元格值)从一个文件夹移动到另一个文件夹

时间:2019-03-11 21:48:03

标签: excel vba

实际上,我正在搜索将excel文件从一个文件夹移动到另一个文件夹的代码(如果有办法的话),请有人帮我。 列“ A”包含“源路径”列“ B”具有“目标路径”,而列“ C”具有源路径文件夹中的文件名 我需要的是,如果A列的值不等于B列的值,则C列中的各个文件应移至目标文件夹(目标文件夹)

我正在尝试这段代码,但是它将移动源文件夹中存在的所有文件。我将感谢您

Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileExt As String
Dim FNames As String
Dim V As Integer
Dim TotalRow As Integer

TotalRow = ActiveSheet.UsedRange.Rows.Count

For V = 1 To TotalRow
    ' Get value of each row in columns 2 start at row 2
    s = Cells(V + 1, 2).Value
    FromPath = "C:\Users\Adiba\Desktop\Folder A\"  '<< Change
    ToPath = "C:\Users\Adiba\Desktop\Folder B\" '<< Change only the destination folder

    FileExt = "*.xls*"   '<< Change
    'You can use *.* for all files or *.doc for word files

    If Right(FromPath, 1) <> "\" Then
        FromPath = FromPath & "\"
    End If

    FNames = Dir(FromPath & FileExt)

    If Len(FNames) = 0 Then
        MsgBox "No files in " & FromPath
        Exit Sub
    End If

    Set FSO = CreateObject("scripting.filesystemobject")
    FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath
    MsgBox "You can find the files from " & FromPath & " in " & ToPath
Next

0 个答案:

没有答案