I have the following code which moves specified filenames between a source folder and destination folder.
Could someone help me out and update the code so that i can move a file with only the beginning of a file name in column A.
e.g
If a file is named: Filename_123_ABC_.pdf or Filename_123_DEF
I want to be able to provide the first part of the filename as Filename_123 so that both filenames above are moved between the source and destination folder.
Could someone help me out and update the code so that i can move a file with only the beginning of a file name in column A.
e.g
If a file is named: Filename_123_ABC_.pdf or Filename_123_DEF
I want to be able to provide the first part of the filename as Filename_123 so that both filenames above are moved between the source and destination folder.
VBA Code:
Sub MoveFilesBasedOnList()
Dim fso As Object
Dim sourceFileName As String, sourceFolderPath As String
Dim destinFileName As String, destinFolderPath As String
Dim fileName As String, destinFolderName As String
Dim lastRow As Long, x As Long
Set fso = CreateObject("Scripting.FileSystemObject")
sourceFolderPath = Range("B3").Value
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
For x = 3 To lastRow
fileName = Range("A" & x).Value & ".pdf"
destinFolderName = Range("C" & x).Value
sourceFileName = sourceFolderPath & "\" & fileName
destinFolderPath = destinFolderName & "\" & fileName
If Not fso.FolderExists(destinFolderName) Then
MsgBox ("Destination folder " & destinFolderName & " does not exist.")
GoTo NextFile
End If
If Not fso.FileExists(sourceFileName) Then
MsgBox ("File Not Found in " & sourceFileName)
Else
If fso.FileExists(destinFolderPath) Then
MsgBox (destinFolderPath & " already exists. Skipping file " & fileName)
Else
fso.MoveFile Source:=sourceFileName, Destination:=destinFolderPath
MsgBox (sourceFileName & " moved to " & destinFolderPath)
End If
End If
NextFile:
Next x
End Sub