使用VBA FileSystemObject,指定文件的文件扩展名

我正在使用下面的代码来从文件夹及其子文件夹中列出所有带有xls,xlsx或xlsm扩展名的文件。 下面的代码工作,但问题是,它列出所有从子文件夹扩展名的文件,但只列出主文件夹中的Excel文件。 我无法弄清楚这个代码有什么问题。 你可以帮我吗?

Sub List_XL_Files(ByVal SheetName As String, ByVal SourceFolderName As String, ByVal IncludeSubfolders As Boolean) Dim FSO As Object Dim SourceFolder As Object Dim SubFolder As Object Dim FileItem As Object Dim lRoMa As Long Set FSO = CreateObject("Scripting.FileSystemObject") Set SourceFolder = FSO.GetFolder(SourceFolderName) lRoMa = ThisWorkbook.Sheets(SheetName).Cells(Rows.Count, 2).End(xlUp).Row + 1 ReDim arrFolders(ctr) With ThisWorkbook.Sheets(SheetName) For Each FileItem In SourceFolder.Files strFileExt = FSO.GetExtensionName(FileItem) If strFileExt = "xlsm" Or strFileExt = "xlsx" Or strFileExt = "xls" Then MsgBox strFileExt .Cells(lRoMa + r, 1).Value = lRoMa + r - 7 .Cells(lRoMa + r, 2).Formula = strFileExt .Cells(lRoMa + r, 3).Formula = FileItem.Name .Cells(lRoMa + r, 4).Formula = FileItem.Path .Cells(lRoMa + r, 5).Value = "-" .Cells(lRoMa + r, 6).Value = "" .Cells(lRoMa + r, 7).Value = "" r = r + 1 ' next row number X = SourceFolder.Path End If Next FileItem End With If IncludeSubfolders Then For Each SubFolder In SourceFolder.SubFolders ListFilesInFolder SheetName, SubFolder.Path, True Next SubFolder End If Set FileItem = Nothing Set SourceFolder = Nothing Set FSO = Nothing End sub 

谢谢

将以下代码添加到For Each SubFolder In SourceFolder.SubFolders之后

 Call List_XL_Files(SheetName, SubFolder.Path, True) 

它会工作