Macro to select latest modified File

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,591
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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
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,223,705
Messages
6,173,985
Members
452,540
Latest member
haasro02

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