Macro to select latest modified File

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,585
Office Version
  1. 2021
Platform
  1. Windows
I have code below that allows a user to limit the selection when browsing for a csv file in C:\Extract



I would like the code to allow the browsing to show files like MGU Parts or Parts MGU and only show the latest modified files like this


Kindly amend my code





Code:
 Sub Open_Workbook()
    ChDir ("C:\extract")
    Dim nb As Workbook, tw As Workbook, WS As Worksheet, LR As Long, A As Variant
    Dim rngDestination As Range
    Dim filename As Variant
    Set WS = ThisWorkbook.Sheets("Imported Data")
    WS.UsedRange.ClearContents

    On Error Resume Next
    Set rngDestination = WS.Range("A1")
    On Error GoTo 0
    If rngDestination Is Nothing Then Exit Sub  'User canceled

    

    With Application.FileDialog(msoFileDialogFilePicker)
        .ButtonName = "Select"
        With .Filters
            .Clear
            .Add "CSV Files", "*.csv"
        End With
        .FilterIndex = 1
        .InitialFileName = "C:\Extract\MGU*Part*.csv"
        .Title = "Select CSV file"
        If .Show = 0 Then Exit Sub
        filename = .SelectedItems(1)
    End With
    Debug.Print filename

    Application.ScreenUpdating = False
    Dim srcWorkbook As Workbook
    Set srcWorkbook = Workbooks.Open(filename:=filename, local:=True)
    ThisWorkbook.Activate

    srcWorkbook.Sheets(1).Range("A:AM").Copy
    rngDestination.PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False

    srcWorkbook.Close SaveChanges:=False

    Application.ScreenUpdating = True
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
You cannot tell FileDialog to show only recently modified files. If you need such functionality, a custom UserForm can be created to accommodate to your needs.
 
Upvote 0
Many thanks for the link. This will certainly help
 
Upvote 0
You're welcome!
hi.gif
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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