Macro to Cut and Paste Files containing name "Purchase Ledger" from one folder to another

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
Office Version
  1. 2021
Platform
  1. Windows
I have a macro to Cut a and paste workbooks from one folder to another containing "purchase Ledger" ,except the current workbook

When running the macro the Files containing "Purchase Ledger" is not being moved i.e Cut and Pasted

It would be appreciated if someone could amend my code

Code:
 Sub Move_Files_With_Purchase_Ledger()
    Dim sourceFolder As String
    Dim destinationFolder As String
    Dim fileKeyword As String
    Dim fileName As String
    Dim fso As Object
    
    sourceFolder = "C:\My Documents"
    destinationFolder = "C:\Old My Documents"
    fileKeyword = "Purchase Ledger"
    
    ' Create the FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' Iterate through files in the source folder
    fileName = Dir(sourceFolder & "\*.*")
    Do While fileName <> ""
        If InStr(1, fileName, fileKeyword, vbTextCompare) > 0 Then
            ' Move the file to the destination folder
            fso.MoveFile sourceFolder & "\" & fileName, destinationFolder & "\" & fileName
        End If
        fileName = Dir
    Loop
    
    ' Clean up the FileSystemObject
    Set fso = Nothing
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
With some minor modifications to suit my directories and file names, your code works for me. I suggest you at least debug.print your file names contained in the source directory to ensure what you read in that output does in fact contain the string (fileKeyword) that you're looking for.
 
Upvote 0
Thanks for the advise. Will certainly try this
 
Upvote 0
A variation:
VBA Code:
Sub Move_Files_With_Purchase_Ledger()
    Dim sourceFolder As String, destinationFolder As String, fileKeyword As String
    Dim fso As Object, FFolder As Object, FFile As Object
    Dim FCnt As Long, MCnt As Long
    
    sourceFolder = "C:\My Documents"
    destinationFolder = "C:\Old My Documents"
    fileKeyword = "Purchase Ledger"
 
    ' Create the FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    'Check folders
    If Not fso.FolderExists(sourceFolder) Then
        MsgBox "Folder '" & sourceFolder & "' not found. ", vbOKOnly Or vbExclamation, "Source Folder"
        Exit Sub
    End If
    
    If Not fso.FolderExists(destinationFolder) Then
        MsgBox "Folder '" & destinationFolder & "' not found. ", vbOKOnly Or vbExclamation, "Destination Folder"
        Exit Sub
    End If
    
    ' Iterate through files in the source folder
    destinationFolder = Replace(Trim(destinationFolder) & "\", "\\", "\") ' dest folder always needs a "\"

    Set FFolder = fso.GetFolder(sourceFolder)
    
    For Each FFile In FFolder.Files
        FCnt = FCnt + 1
        If InStr(UCase(FFile.Name), UCase(fileKeyword)) > 0 Then
            FFile.Move destinationFolder  'Move files
            MCnt = MCnt + 1
        End If
    Next FFile
    
    MsgBox MCnt & " out of " & FCnt & " files moved", vbInformation
    
    ' Clean up the FileSystemObject
    Set fso = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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