Use the FileSearch property to return the FileSearch object. The following example searches for files and displays the number of files found and the name of each file.
Sub App_FileSearch_Example()
Dim OWB As Workbook
With Application.FileSearch
.NewSearch
.LookIn = "c:\vbadud\delivered_files\"
.FileName = "*.xls"
If .Execute(SortBy:=msoSortByLastModified, SortOrder:=msoSortOrderDescending) > 0 Then
For i1 = 1 To .FoundFiles.Count
Set OWB = Workbooks.Open(.FoundFiles(i1))
OWB.Save
OWB.Close
Next i1
End If
End With
End Sub
Use the NewSearch method to reset the search criteria to the default settings. All property values are retained after each search is run, and by using the NewSearch method you can selectively set properties for the next file search without manually resetting previous property values. The following example resets the search criteria to the default settings before beginning a new search.
The above example will search for all excel files in a particular folder and opens the latest file. This method can also be used to Identify the last-modified or latest file using Excel VBA.
See also:
Open Excel Files - Open Dialog - GetOpenFilename Method
Browse a Folder / Select a Folder Thru Shell
The application.filesearch keeps returning an error. How do I make this work?
ReplyDeleteApplication.FileSearch will throw error if you are using Office 2007. You can use the following workaround :
ReplyDeletehttp://www.ozgrid.com/forum/showthread.php?t=71409
Hi there...Can you post a code that can filter a data in table and returns its value in a notepad.Is it possible?
ReplyDeleteIf filesearch a property or object or both?
ReplyDeleteI need help!! I have tons of excel worksheets in a folder but I want to search and find them quicker and have them automatically open.
ReplyDeleteHi, can excel look in more then one folder ?
ReplyDeleteLet's say I give him a folder were to search, and then there are more folders to search in for the specified file.
Can you modify this VBA to search in more then one folder. Let's say I'll give him the path of the folder to search in, but there he will meet more folders to be searched.
ReplyDeletecan we search all empty folders and files
ReplyDeletecan you modify the script to search word or string inside a files present in the folder
ReplyDeleteI need to sort thousands of excel files into folders based on a name found in the same tab and cell within each file. These need to be moved into a folder with a matching name to the string contents of that cell. Is there a way in vba to do this sorting process for me?
ReplyDeleteWhy don't all you people make an effort to figure these questions yourself. Just because he posted some useful info doesn't make him your lackey. Bunca lazy azzes
ReplyDelete