Copy last file RRS feed

  • Question

  • Hey,

    I would like to find the last file in my folder and copy it. Below I stated my code for finding the last file, however, I don´t know how to redefine the string pName (which is the file) to an object or an application so that I can copy it afterwards.

    Thank you for your help

    Sub LastFileSelect()
    Dim ordner As String
        Dim strTyp As String
        Dim Dateiname As String
        Dim Dateiname_neu As String
        Dim Zeit As Date
        Dim pName As String
    ordner = Range("G5").Value & "\"
        strTyp = "*.xlsx"
        Dateiname = Dir(ordner & strTyp)
        Dateiname_neu = Dateiname
        Zeit = FileDateTime(ordner & Dateiname)
        Do While Dateiname <> ""
            If Zeit < FileDateTime(ordner & Dateiname) Then
                Zeit = FileDateTime(ordner & Dateiname)
                Dateiname_neu = Dateiname
            End If
            Dateiname = Dir
     pName = ordner + Dateiname_neu

    Friday, February 16, 2018 8:15 AM

All replies

  • pName is the complete path of the most recent file. You can use

    FileCopy pName, "new path and filename"

    to copy the file to another location.

    Regards, Hans Vogelaar (

    Friday, February 16, 2018 9:31 AM