Open newest downloaded file when multiple with same partial name

cdalgorta

Board Regular
Joined
Jun 5, 2022
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hi. What would be the macro to look in the downloads folder for all files that start with the partial name "Additional info looker Standard Unlimited*.csv" and then open the "most recent one"?

The code below always opens the oldest created(downloaded) file if there are 2 or more with the same partial name, instead of the newest one. Any way to make this reverse?
Thank you

VBA Code:
Sub YLoadCSV_AdditionalInfo()
Dim CSV_FileToOpen      As String

CSV_FileToOpen = Dir(Environ("USERPROFILE") & "\Downloads\" & _
            "Additional info looker Standard Unlimited*.csv")                                   ' Save found CSV file to CSV_FileToOpen

  Workbooks.Open Filename:=Environ("USERPROFILE") & "\Downloads\" & CSV_FileToOpen            ' Open the CSV file

End Sub


1664063223097.png
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Give this a try

VBA Code:
Option Explicit

'****************************************************************
'*  Purpose: Open the most recent download of file              *
'*  Filename: "Additional info looker Standard Unlimited*.csv"  *
'*  Author: dave3009                                            *
'*  Date: 2022-09-26                                            *
'****************************************************************

Sub LoopThroughFiles()

Dim strFileSpec As String
Dim strFileName As String
Dim cTime As Double
Dim tempFile As String
Dim strFolder As String

Const strSearch As String = "Additional info looker Standard Unlimited" 'change this to suit the filename

strFolder = Environ("USERPROFILE") & "\Downloads\"                      'Folder where you are searching
strFileSpec = strFolder & "*.*"                                         'set a generic specification for all files

strFileName = Dir(strFileSpec)                                          'get the directory

cTime = 0                                                               'set a start point for getting the time

Do While Len(strFileName) > 0                                           'D0 loop over the files
    If Left(strFileName, Len(strSearch)) = strSearch Then                                'test the filename
        If CDbl(FileDateTime(strFolder & strFileName)) > cTime Then     'test the datetime of the file
            cTime = CDbl(FileDateTime(strFolder & strFileName))         'store the details if this is most recent
            tempFile = strFileName
        End If
    End If
    strFileName = Dir
Loop

Workbooks.Open strFolder & tempFile                                     'open the most recent file
End Sub
 
Upvote 0
Solution
Give this a try

VBA Code:
Option Explicit

'****************************************************************
'*  Purpose: Open the most recent download of file              *
'*  Filename: "Additional info looker Standard Unlimited*.csv"  *
'*  Author: dave3009                                            *
'*  Date: 2022-09-26                                            *
'****************************************************************

Sub LoopThroughFiles()

Dim strFileSpec As String
Dim strFileName As String
Dim cTime As Double
Dim tempFile As String
Dim strFolder As String

Const strSearch As String = "Additional info looker Standard Unlimited" 'change this to suit the filename

strFolder = Environ("USERPROFILE") & "\Downloads\"                      'Folder where you are searching
strFileSpec = strFolder & "*.*"                                         'set a generic specification for all files

strFileName = Dir(strFileSpec)                                          'get the directory

cTime = 0                                                               'set a start point for getting the time

Do While Len(strFileName) > 0                                           'D0 loop over the files
    If Left(strFileName, Len(strSearch)) = strSearch Then                                'test the filename
        If CDbl(FileDateTime(strFolder & strFileName)) > cTime Then     'test the datetime of the file
            cTime = CDbl(FileDateTime(strFolder & strFileName))         'store the details if this is most recent
            tempFile = strFileName
        End If
    End If
    strFileName = Dir
Loop

Workbooks.Open strFolder & tempFile                                     'open the most recent file
End Sub
Thank you so much Dave! It worked perfectly!
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top