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

  Рет қаралды 2,539

greggowaffles

greggowaffles

2 жыл бұрын

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 (KZfaq 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

Пікірлер: 5
@mastercamer
@mastercamer 2 жыл бұрын
great video man!
@greggowaffles
@greggowaffles 2 жыл бұрын
Thanks!
@gopalsriharsha7495
@gopalsriharsha7495 Жыл бұрын
If I need to select any other folder apart from Documents Folder, how can I define the Path? right now with program default Docs folder is opening from C drive
@suziedkingery4531
@suziedkingery4531 2 жыл бұрын
I'm completely new to macros and trying to work on one for work. I have it created (based off simple recording/following my movements in excel) and edited basic things in VBA. However I need some help...every day we hit the button and it's supposed to go grab a weather file, the name of this file changes daily with the date...I have no idea where or how I'm supposed to enter what you're saying here off my information I already had in there....or how it knows where to go with just the mypath myfile my folder ☹️
@greggowaffles
@greggowaffles 2 жыл бұрын
i can help. what does your code look like?
Alex hid in the closet #shorts
00:14
Mihdens
Рет қаралды 8 МЛН
아이스크림으로 체감되는 요즘 물가
00:16
진영민yeongmin
Рет қаралды 59 МЛН
Survival skills: A great idea with duct tape #survival #lifehacks #camping
00:27
Cat Corn?! 🙀 #cat #cute #catlover
00:54
Stocat
Рет қаралды 16 МЛН
Excel VBA Macro: List All Files (From a Selected Folder)
8:43
greggowaffles
Рет қаралды 9 М.
VBA to BROWSE & COPY Data from SELECTED File in Excel
10:00
Leila Gharani
Рет қаралды 323 М.
VBA Open File Name with Date
11:56
The Excel Cave
Рет қаралды 6 М.
How NVIDIA just beat every other tech company
9:20
Mrwhosetheboss
Рет қаралды 1,1 МЛН
Excel VBA Open Files in Path and Edit
13:33
The Excel Cave
Рет қаралды 11 М.
Loop Through All Files in a Folder Using VBA/Macros in Excel
13:41
Alex hid in the closet #shorts
00:14
Mihdens
Рет қаралды 8 МЛН