79505453

Date: 2025-03-13 05:00:39
Score: 3.5
Natty:
Report link

I have the following excel VBA, which finds files based on an excel list and copies then from the source folder to the destination folder.

But, I need to be able to copy the file based on a file containing certain text, i.e. my excel list has QTMP-1 as a file name but I want it to identify and copy all files (there may be more than one) that contain QTMP-1 before moving to the next search item in my list.

The code below is based on an exact match (and assumes 1 list item to 1 file). What do I need to change?

Sub copyfiles()
'Updateby http://www.hardipdabhi.wordpress.com
'Copies files from one folder to another based on a list in excel
    Dim xRg As Range, xCell As Range
    Dim xSFileDlg As FileDialog, xDFileDlg As FileDialog
    Dim xSPathStr As Variant, xDPathStr As Variant
    Dim xVal As String
    On Error Resume Next
    Set xRg = Application.InputBox("Please select the file names:", "www.hardipdabhi.wordpress.com", ActiveWindow.RangeSelection.Address, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    Set xSFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
    xSFileDlg.Title = "Please select the original folder:"
    If xSFileDlg.Show <> -1 Then Exit Sub
    xSPathStr = xSFileDlg.SelectedItems.Item(1) & "\"
    Set xDFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
    xDFileDlg.Title = "Please select the destination folder:"
    If xDFileDlg.Show <> -1 Then Exit Sub
    xDPathStr = xDFileDlg.SelectedItems.Item(1) & "\"
    For Each xCell In xRg
        xVal = xCell.Value
        If TypeName(xVal) = "String" And xVal <> "" Then
            FileCopy xSPathStr & xVal, xDPathStr & xVal
        End If
    Next
End Sub
Reasons:
  • Blacklisted phrase (0.5): I need
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Andrew