List Time Last Saved (For All Files In Folder) | Excel VBA Macro

Опубликовано: 07 Октябрь 2024
на канале: greggowaffles
1,363
20

Excel VBA Macro: List Time Last Saved (For All Files In Folder). In this video, we go over how to automatically list the file names and last saved times for all files in a user selected folder. We also have the macro show the file location, and create a message for the user if there are no files in the selected folder.

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

Sub list_last_save_times()

Dim myPath As String
Dim myFile As String
Dim FldrPicker As FileDialog
Dim sh As Worksheet
Dim i As Integer

Application.ScreenUpdating = False

Set sh = ThisWorkbook.Sheets("Last Save Times")
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

sh.Activate
sh.Cells.ClearContents
Cells(1, 1) = "File Name(s)"
Cells(1, 2) = "Last Time Saved"
Cells(1, 4) = "Location:"
Cells(1, 5) = myPath

myFile = Dir(myPath)
i = 1

Do While myFile LTGT ""

sh.Cells(i + 1, 1) = myFile
Workbooks.Open Filename:=myPath & myFile
With sh.Cells(i + 1, 2)
.Value = ActiveWorkbook.BuiltinDocumentProperties("Last Save Time")
.NumberFormat = "mm-dd-yyyy h:mm AM/PM"
End With
ActiveWorkbook.Close savechanges:=False
myFile = Dir
i = i + 1

Loop

sh.Range("A:B").Columns.AutoFit

If i = 1 Then

MsgBox "There are no items in this folder"
End If

Application.ScreenUpdating = True

End Sub

#ExcelVBA #ExcelMacro