(Solved) – copy files after a user specified file name


Im trying to copy files after a user specified file name. I have folder that updates every day with hundreds of files in it listed in numerical order with the format 4129_XXXX.txt. Where XXXX is the only number that changes for instance if 4129_0001.txt is the first file the next would be 4129_0002.txt and so on. I have a separate macro that pulls all the information I want from the txt files but I have to manually copy all the new incidents from the folder and save them into my directory to run my data sorting macro on them. I want to set it up so that I can simply type in the last incident and the macro pulls all the files after the user specified file and automatically copies them to my directory so I can run my data sorting macro. My code is below I have it set up where the user can type in the last incident seen on the data sorted macro excel sheet from the previous day. My goal was then to have the code pull all the files after the number the user typed in however my code simply continues to copy all the files in the folder. I could really use some help in solving this issue.

Sub sort_read()

Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileInFromFolder As Object
Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String
Dim MyFolder As String
Dim MyFile As String
Dim wb As Workbook
Dim myValue As Variant

myValue = InputBox("4129_XXXX.txt")
MyFolder = "C:UsersDesktopLogbook Source Files6817 scanner"
MyFile = Dir(MyFolder & "myValue")
If MyFolder > myValue Then
    FromPath = MyFolder
    ToPath = "C:UsersDesktopLogbook Source Files4129 scanner"

    If Right(FromPath, 1) = "" Then
        FromPath = Left(FromPath, Len(FromPath) - 1)
    End If

    If Right(ToPath, 1) = "" Then
        ToPath = Left(ToPath, Len(ToPath) - 1)
    End If

    Set FSO = CreateObject("scripting.filesystemobject")

    If FSO.FolderExists(FromPath) = False Then
        MsgBox FromPath & " doesn't exist"
        Exit Sub
    End If

    FSO.CopyFolder Source:=FromPath, Destination:=ToPath
    MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
End If

End Sub 

Leave a Reply

Your email address will not be published. Required fields are marked *