Search .xlsx files in folder, starting with most recent modification.

Arithos

Well-known Member
Joined
Aug 14, 2014
Messages
598
Hi,

This is my basic template to search through a specific file type within a folder.

Code:
ChDir ("Your Directory Here")

'Selecting a folder to search through. 
With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Please select a folder"
            .Show
            .AllowMultiSelect = False
   If .SelectedItems.Count = 0 Then
            MsgBox "You did not select a folder"
        Exit Sub
   End If
           fPath = .SelectedItems(1) & "\"
End With


If Right(fPath, 1) <> "\" Then fPath = fPath & "\"
fName = Dir(fPath & "*.xlsx") 'alternatives, .xlsm, . txt etc...


    Do
        fName = Dir
        If fName = "" Then
            Exit Sub
        End If
        
            Set wbk = Workbooks.Open(fPath & fName)
                
            'Macro while in workbook


                  wbk.close
    Loop While fName <> ""

However, while using this, and there could be up to 100 workbooks per folder. And as my title suggests I would like the "search" through these files to start with the files most recently modified. From what I can tell, it starts with the "oldest" files within the folders at the moment.

Does anyone know if this is at all possible, and if it is, how to amend my "search"?

If anything is unclear, please let me know and I will try to clarify.


EDIT: In the specific case that made me want this change, I want to search all cells within a wbk for a specific value. If a solution for this is easily available, I'm all ears. I'm mainly thinking speed of the search.
 
Last edited:

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.

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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