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
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