Tuesday, May 15, 2012

Copy specific list of files form a group of files to specified folder

This macro will help you to copy specific list of files form a folder containing many files.

Code:
*********************************************************************
Private Sub CopyFiles()
    Dim myfilesystemobject As Object
    Dim myfiles As Object
    Dim myfile As Object
    Dim rng As Range
    Dim Cell As Object
    Dim strDirectory As String
    Dim strDestFolder As String
   
    strDirectory = InputBox("Enter Source Folder")
    strDestFolder = InputBox("Enter Destination Folder")
   
    Set rng = Application.InputBox(prompt:="Select the cells containg the file names", Title:="Select Range of Cells", Type:=8) 'set this to the range of your filtered list
    Set myfilesystemobject = CreateObject("Scripting.FileSystemObject")
    Set myfiles = myfilesystemobject.GetFolder(strDirectory).Files
       
    On Error Resume Next
    For Each Cell In rng
        For Each myfile In myfiles
            If Not IsNull(Cell.Value) Then
                If myfile = strDirectory & "\" & Cell.Value & ".tagged.xml" Then
                    With myfile
                        .Copy strDestFolder & "\" & myfile.Name
                    End With
                Else
                End If
            End If
        Next myfile
    Next Cell
End Sub
*********************************************************************

1 comment:

  1. works great, but where would I modify your code if I have the file extension as part of the filename in the list (for example, the list contains 1003.csv, not 1003)?

    ReplyDelete