How to Open Most Recent File from Specific Folder

How to open the most recent file from a specific folder automatically with VBA. Using this macro you can also specify which types of files you would like to open. You may like to view the most recent Excel files or PDF files or files with specific extensions. Watch the training video below and then refer to the VBA code:

Here’s the complete VBA macro code to display all recent Excel files from the invoices folder on the local disk.

Sub recentFilesSpecificFolder()
Dim myFile As String
Dim myRecentFile As String
Dim myMostRecentFile As String
Dim recentDate As Date
Dim myDirectory As String
myDirectory = “C:\invoices\”
Dim fileExtension As String
fileExtension = “*.xls”

If Right(myDirectory, 1) <> "\" Then myDirectory = myDirectory & "\"

'myFile = Dir(myDirectory & fileExtension, 0)
myFile = Dir(myDirectory & fileExtension)
'MsgBox myFile
If myFile <> "" Then
    myRecentFile = myFile
    recentDate = FileDateTime(myDirectory & myFile)
    Do While myFile <> ""
        If FileDateTime(myDirectory & myFile) > recentDate Then
             myRecentFile = myFile
             recentDate = FileDateTime(myDirectory & myFile)
         End If
         myFile = Dir
    Loop
End If
myMostRecentFile = myRecentFile
Workbooks.Open Filename:=myDirectory & myMostRecentFile

End Sub

You can view this video on YouTube.

Sample for download and practice

Published by

Dinesh Kumar Takyar

Welcome to exceltrainingvideos.com! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: http://youtube.com/familycomputerclub For a structured Excel VBA training course online you can visit: https://www.youtube.com/excelvbaonline

One thought on “How to Open Most Recent File from Specific Folder”

  1. good evening. i am a new user of excel vba. how can save invoice as pdf and and invoice itam deduct from total stock and how new stock automatically add in stock.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.