+ Reply to Thread
Results 1 to 7 of 7

Open most recently CREATED file in a folder

Hybrid View

  1. #1
    Registered User
    Join Date
    02-27-2007
    Location
    Jackson, MS
    MS-Off Ver
    365
    Posts
    96

    Open most recently CREATED file in a folder

    The following macro opens the most recently modified file in a folder. How can I get it to open the most recently created file?

       Dim MyPath As String
        Dim MyFile As String
        Dim LatestFile As String
        Dim LatestDate As Date
        Dim LMD As Date
        MyPath = "\CustView"
        If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
        MyFile = Dir(MyPath & "*.xlsx", vbNormal)
        If Len(MyFile) = 0 Then
            MsgBox "No files were found...", vbExclamation
            Exit Sub
        End If
        Do While Len(MyFile) > 0
            LMD = FileDateTime(MyPath & MyFile)
            If LMD > LatestDate Then
                LatestFile = MyFile
                LatestDate = LMD
            End If
            MyFile = Dir
        Loop
        Workbooks.Open MyPath & LatestFile
        Range("A1").Select
    Last edited by go3go3go; 01-16-2021 at 09:44 AM.

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Open most recently CREATED file in a folder

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  3. #3
    Registered User
    Join Date
    02-27-2007
    Location
    Jackson, MS
    MS-Off Ver
    365
    Posts
    96

    Re: Open most recently CREATED file in a folder

    Sorry about that.

  4. #4
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Open most recently CREATED file in a folder

    Ok. Please add this UDF :
    Function GetFileDateLastModified(strFile)
      GetFileDateLastModified = CreateObject("Scripting.FileSystemObject").GetFile(strFile).DateLastModified
    End Function
    Function GetFileDateCreated(strFile)
      GetFileDateCreated = CreateObject("Scripting.FileSystemObject").GetFile(strFile).DateCreated
    End Function
    And then change this line into :
    'LMD = FileDateTime(MyPath & MyFile)
    LMD = GetFileDateCreated(MyPath & MyFile)
    As a note, there could be unexpected desired on how Windows treat the file creation and modification date.
    Lets say a file "ABC.TXT" is created on 1 Jan 2021, and it has modified several times and the last modification date is 15 Jan 2021.
    Now, as long as the file stay in the folder it is placed, then it will be working as expected -> the modification date will always bigger than creation date
    But if this file "ABC.TXT" is copied to another folder, then for this copied file, the modification date will be the same but the creation date will be set as the current time the file being copied, so for this copied file, the creation date will be bigger than modification date.
    The macro above just get the creation date reported by Operating System (you can check this by examining the file from Windows Explorer). Should this not what you expected, you need to change the code to fulfill your expectation.

  5. #5
    Registered User
    Join Date
    02-27-2007
    Location
    Jackson, MS
    MS-Off Ver
    365
    Posts
    96

    Re: Open most recently CREATED file in a folder

    Quote Originally Posted by karedog View Post
    Ok. Please add this UDF :
    Function GetFileDateLastModified(strFile)
      GetFileDateLastModified = CreateObject("Scripting.FileSystemObject").GetFile(strFile).DateLastModified
    End Function
    Function GetFileDateCreated(strFile)
      GetFileDateCreated = CreateObject("Scripting.FileSystemObject").GetFile(strFile).DateCreated
    End Function
    And then change this line into :
    'LMD = FileDateTime(MyPath & MyFile)
    LMD = GetFileDateCreated(MyPath & MyFile)
    As a note, there could be unexpected desired on how Windows treat the file creation and modification date.
    Lets say a file "ABC.TXT" is created on 1 Jan 2021, and it has modified several times and the last modification date is 15 Jan 2021.
    Now, as long as the file stay in the folder it is placed, then it will be working as expected -> the modification date will always bigger than creation date
    But if this file "ABC.TXT" is copied to another folder, then for this copied file, the modification date will be the same but the creation date will be set as the current time the file being copied, so for this copied file, the creation date will be bigger than modification date.
    The macro above just get the creation date reported by Operating System (you can check this by examining the file from Windows Explorer). Should this not what you expected, you need to change the code to fulfill your expectation.
    Looks like that did it. Thank you very much!!!!!!!!!!!!!!

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2406 (Windows 11 23H2 64-bit)
    Posts
    82,684

    Re: Open most recently CREATED file in a folder

    Administrative Note:

    Please don't quote whole posts, especially when you are responding to the one immediately preceding your own - it's just clutter. It's OK to quote if you are responding to a post out of sequence, but limit quoted content to a few relevant lines that makes clear to whom and what you are responding. Thanks!

    For normal conversational replies, try using the QUICK REPLY box below or the REPLY button instead of REPLY WITH QUOTE.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  7. #7
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Open most recently CREATED file in a folder

    You are welcome, thanks for marking the thread as solved.

    Regards

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to open most recently opened folder
    By kbenjamin827 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-11-2019, 08:20 AM
  2. Macro to email in Outlook the last file created in a folder
    By rhudgins in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-23-2017, 04:46 PM
  3. macro to open most recently modified .CSV file
    By Newsome79 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-21-2016, 01:30 PM
  4. Outlook macro to attach most recently created file from folder
    By lsargent in forum Outlook Programming / VBA / Macros
    Replies: 0
    Last Post: 08-17-2015, 11:45 AM
  5. save as file, into folder just created
    By mike02 in forum Excel General
    Replies: 1
    Last Post: 08-15-2012, 08:19 PM
  6. Function to Save file in A Created Folder
    By jo15765 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-19-2012, 01:07 PM
  7. Creating a file in a newly created folder.
    By AnthonyWB in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 04-12-2010, 03:01 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1