我有场景,我需要根据部分文件名将文件移动到另一个位置。例如,“FAI 741727-001 SMS CQ 6U PASS 061217.xlsx”是文件名,我想创建另一个位置为6U,然后将该文件移动到该文件夹。excel vba根据部分文件名移动文件
我有一个代码,可以帮助我将文件移动到一个文件夹只有当我给完整的文件名。是否有人可以帮助我在这..
代码:
Sub MoveFiles()
Dim SourcePath As String
Dim DestPath As String
Dim FileName As String
Dim LastRow As Long
Dim i As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LastRow
FileName = Cells(i, "B").Value
If Right(Cells(i, "A").Value, 1) <> Application.PathSeparator Then
SourcePath = Cells(i, "A").Value & Application.PathSeparator
Else
SourcePath = Cells(i, "A").Value
End If
If Right(Cells(i, "C").Value, 1) <> Application.PathSeparator Then
DestPath = Cells(i, "C").Value & Application.PathSeparator
Else
DestPath = Cells(i, "C").Value
End If
If Dir(SourcePath & FileName) = "" Then
Cells(i, "D").Value = "Source file does not exist."
ElseIf Dir(DestPath & FileName) <> "" Then
Cells(i, "D").Value = "File already exists."
Else
Name SourcePath & FileName As DestPath & FileName
Cells(i, "D").Value = "File moved to new location"
End If
Next i
End Sub
那么,你希望你的文件被移动到哪里?它与工作簿是相同的目录吗?还是别的?基于目录的名称是什么? –
批处理文件/ PowerShell?可能会更简单。 [BTW](https://stackoverflow.com/help/tagging)。 – pnuts
@ Michal Turczyn - 我需要在B列 – Kelvin