FileSearch to Exclude certain filename & foldername from

Adrae

Active Member
Joined
Feb 19, 2002
Messages
306
Hi - I have the following code to list files from a directory. What I would like is to have it skip over\exclude the filename of the spreadsheet containing the macro (ThisWorkbook) and the folders calles "ABC" and "DEF".

Is that possible?

Sub MyFilesearch()
Dim fs As Object
Dim MyPath As String
Dim FileToSearchFor As String
Dim MyFileName As String
Dim ws As Worksheet
Dim FileNumber As Integer
'--------------------------------------------------------------
'- initialise variables
Set ws = Worksheets("FileList")
ws.Cells.Clear
MyPath = ThisWorkbook.Path
FileToSearchFor = "*.xls"
Set fs = Application.FileSearch
'----------------------------------------------------------------
With fs
.LookIn = MyPath
.Filename = FileToSearchFor
.SearchSubFolders = True
If .Execute > 0 Then
'---------------------------------
'- get file names
For FileNumber = 1 To .FoundFiles.Count
MyFileName = WorksheetFunction.Substitute(.FoundFiles(FileNumber), MyPath, "")
ws.Cells(FileNumber, 1).Value = MyFileName
Next
'----------------------------------------------------------
Else
MsgBox "There were no files found."
Exit Sub
End If
End With
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,136,612
Messages
5,676,796
Members
419,651
Latest member
alexanderguhr

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
Top