Amended code to look at latest .xlsx or csv file

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,589
Office Version
  1. 2021
Platform
  1. Windows
I would like to amend code below to include .xlsx or .csv file, and import whichever is the latest file

Your assistance is most appreciated


Code:
 Sub OpenLatestFile()

   
   Dim MyPath As String
    Dim MyFile As String
    Dim LatestFile As String
    Dim LatestDate As Date
    Dim LMD As Date
    Dim lngLastRow As Long
    
    'Specify the path to the folder
  MyPath = "C:\Sales_extract"

    'Make sure that the path ends in a backslash
    If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"

    'Get the first Excel file from the folder
    MyFile = Dir(MyPath & "*sales*BR*.xlsx", vbNormal)

    'If no files were found, exit the sub
    If Len(MyFile) = 0 Then
        MsgBox "No files were found...", vbExclamation
        Exit Sub
    End If

    'Loop through each Excel file in the folder
    Do While Len(MyFile) > 0
    
        'Assign the date/time of the current file to a variable
        LMD = FileDateTime(MyPath & MyFile)
        
        'If the date/time of the current file is greater than the latest
        'recorded date, assign its filename and date/time to variables
        If LMD > LatestDate Then
            LatestFile = MyFile
            LatestDate = LMD
        End If
        
        'Get the next Excel file from the folder
        MyFile = Dir
        
    Loop
    
    'Open the latest file
    Workbooks.Open MyPath & LatestFile
    
    With Sheets(1)
        lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        .Range("A3:O" & lngLastRow).Copy Destination:=ThisWorkbook.ActiveSheet.Range("A2")
        Application.CutCopyMode = False
    End With
    
    ActiveWorkbook.Close savechanges:=False 'Close the *.csv without saving
    
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
First, change the filter for your files as follows . . .

VBA Code:
    'Get the first Excel file from the folder
    MyFile = Dir(MyPath & "*sales*BR*.*", vbNormal)

Then, just in case you have other files with the same naming convention but a different extension, include an IF statement within your loop to filter for .xlsx and .csv files . . .

VBA Code:
    'Loop through each Excel file in the folder
    Do While Len(MyFile) > 0
   
        If LCase(Right(MyFile, 5)) = ".xlsx" Or LCase(Right(MyFile, 4)) = ".csv" Then
       
            'Assign the date/time of the current file to a variable
            LMD = FileDateTime(MyPath & MyFile)
           
            'If the date/time of the current file is greater than the latest
            'recorded date, assign its filename and date/time to variables
            If LMD > LatestDate Then
                LatestFile = MyFile
                LatestDate = LMD
            End If
           
        End If
           
        'Get the next Excel file from the folder
        MyFile = Dir
       
    Loop

Hope this helps!
 
Upvote 0
Solution
Many thanks for the help. Code works perfectly
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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