Excel VBA Macro: Open Newest File (In User Selected Folder)

Описание к видео Excel VBA Macro: Open Newest File (In User Selected Folder)

Excel VBA Macro: Open Newest File (In User Selected Folder). In this video, we go over how to automatically find and open the latest file in a user selected folder.

Code (YouTube doesn't allow brackets; so LT and GT are used for less than and greater than, respectively):

Sub newest_file_in_folder()

Dim FldrPicker As FileDialog
Dim myPath As String
Dim myFile As String
Dim newestFile As String
Dim newestDate As Date

Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

With FldrPicker
.Title = "Please Select Folder"
.AllowMultiSelect = False
.ButtonName = "Confirm"
If .Show = -1 Then
myPath = .SelectedItems(1) & "\"
Else
End
End If
End With

myFile = Dir(myPath)
newestFile = myFile

On Error GoTo noFiles
newestDate = FileDateTime(myPath & myFile)

Do While myFile LTGT ""

If FileDateTime(myPath & myFile) GT newestDate Then
newestFile = myFile
newestDate = FileDateTime(myPath & myFile)
End If

myFile = Dir

Loop

Workbooks.Open Filename:=myPath & newestFile
End

noFiles:

MsgBox "There are no items in this folder."

End Sub

#ExcelVBA #ExcelMacro

Комментарии

Информация по комментариям в разработке