我有場景,我需要根據部分文件名將文件移動到另一個位置。例如,「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