Help updating VBA code to use wildcard in the file name

WH12TTY

New Member
Joined
Dec 2, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
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.

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
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try this (not tested).

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
        destinFolderName = Range("C" & x).Value
        If Not fso.FolderExists(destinFolderName) Then
            MsgBox ("Destination folder " & destinFolderName & " does not exist.")
        Else
          
            fileName = Range("A" & x).Value & "*.pdf"
          
            sourceFileName = Dir(sourceFolderPath & "\" & fileName)
          
            Do While sourceFileName <> ""
               DoEvents 
              
                destinFolderPath = destinFolderName & "\" & sourceFileName
              
                If fso.FileExists(destinFolderPath) Then
                    MsgBox (destinFolderPath & " already exists. Skipping file " & sourceFileName)
                Else
                    sourceFileName = sourceFolderPath & "\" & sourceFileName
                    fso.MoveFile Source:=sourceFileName, Destination:=destinFolderPath
                    MsgBox sourceFileName & vbLf & " moved to " & vbLf & destinFolderPath, vbInformation, "File Moved"
                End If
                sourceFileName = Dir()
            Loop
        End If
    Next x
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,183
Members
452,893
Latest member
denay

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top