Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

applicationfilesearch

Forum home » Delegate support and help forum » Microsoft Excel Training and help » Application.Filesearch

Application.Filesearch

ResolvedVersion 2007

Jonathan has attended:
No courses

Application.Filesearch

Hi there,
I had written a macro in Excel 2003 which searches a directory and lists all of the files in that directory, of type *.ssn and writes this list to a worksheet. To do this i used the Application.Filesearch functionality. Having upgraded to Excel 2007 this no longer works and it would appear the Filesearch functionality has been removed. The code i used is below - could you suggest how i can get this working in Excel 2007 please. Note I no longer need it to be able to work in Excel 2003.
Regards,
Jonathan

Sub ListAllFiles()


Dim fs As FileSearch
Dim ws As Worksheet
Dim i As Long


Set fs = Application.FileSearch


'Clear the current File List
Sheets("MISC").Range("AL49:AL128").ClearContents


'Create a new File List - all *.ssn files in the strDataLocation data directory
With fs

.SearchSubFolders = False ' set to true if you want sub-folders included
'.FileType = msoFileTypeAllFiles 'can modify to just Excel files eg with msoFileTypeExcelWorkbooks
.Filename = "*.ssn" 'Can use wildcards for file types.
.LookIn = strDataLocation 'modify this to where you want to serach

If .Execute > 0 Then

Set ws = Worksheets("MISC")

For i = 1 To .FoundFiles.Count

ws.Cells(i + 48, 38) = Mid$(.FoundFiles(i), InStrRev(.FoundFiles(i), "\") + 1)

Next

Else

MsgBox "No files found"

End If

End With


End Sub

Edited on Wed 27 Jan 2010, 14:04

RE: Application.Filesearch

Hi Jonathon

Thanks for your question

The removal of the filesearch method in 2007 is annoying, and puzzling.

It is possible to use the Dir method, which is very old dating back to pre windows versions of basic. It is hard to give a precise example of how to use this as I would need to know more about your file structures etc. But I suggest some sort of loop , using the Dir method, that looks at each file in turn and then writes it to the sheet if its extension matches your category.

I have found the following link that outlines some uses of the dir method, which you might find useful.

http://www.ozgrid.com/forum/showpost.php?p=369573& ;postcount=4

If you have any further queries please do not hesitate to get back to me.

Regards

Stephen

Tue 2 Feb 2010: Automatically marked as resolved.

Excel tip:

Quickly copy a formula across sheets

Suppose you have a formula in cell Sheet1!B2, say =A1*5%, that you wish to copy to cell B2 on Sheet2, Sheet3 and Sheet4. Instead of using copy and paste, try this: (1) Select Sheet1!B2. (2) Group Sheet1 with the worksheets Sheet2, Sheet3 and Sheet4 by holding down Ctrl and clicking on the tabs of the sheets to group them. (3) Press the F2 key, then immediately press Enter to copy the formula in Sheet1!B2 across the grouped sheets.

Remember to ungroup the sheets afterwards! Right-click on any tab and choose Ungroup Sheets to do that.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.11 secs.