Hello! Long time since last post.
I'm struggling with this code to list all the files from a specified folder. The code works great, but I'm trying to figure out what I need to alter to return only the names & file types, without having to see the subfolders. If I know that the files are in say G:, I don't want to see the path in front of all the file names.
Function CreateFileList(FileFilter As String, _
IncludeSubFolder As Boolean) As Variant
Dim FileList() As String, FileCount As Long
CreateFileList = ""
Erase FileList
If FileFilter = "" Then FileFilter = "*.*"
With Application.FileSearch
.NewSearch
.LookIn = "G:\mywork"
.FileName = FileFilter
.SearchSubFolders = False
.FileType = msoFileTypeAllFiles
If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) = 0 Then Exit Function
ReDim FileList(.FoundFiles.Count)
For FileCount = 1 To .FoundFiles.Count
FileList(FileCount) = .FoundFiles(FileCount)
Next FileCount
.FileType = msoFileTypeExcelWorkbooks
End With
CreateFileList = FileList
Erase FileList
End Function
Sub TestCreateList()
Dim FileNamesList As Variant, i As Integer
FileNamesList = CreateFileList("*.*", False)
Range("A:A").ClearContents
Range("A1").Select
Selection.Font.Bold = True
Selection.Font.Underline = xlUnderlineStyleSingle
ActiveCell.FormulaR1C1 = "Files"
Range("H1").Select
For i = 1 To UBound(FileNamesList)
Cells(i + 1, 1).Formula = FileNamesList(i)
Next i
End Sub
Thanks for the help
Bookmarks