从不同的子文件夹访问VBA导入特定的Excel文件

我有一个问题,我找不到正确的代码来做到这一点。

所以我有一个主文件夹(C:\产品)与多个子文件夹对应不同的产品(C:\产品\巧克力,C:\产品\牛奶等等)。 每个子文件夹都有很多的excel文件,但我只想导入一个名为sells.xlxs的文件。 每个子文件夹都有一个sells.xlxs,我想将所有的sells.xlxs导入一个访问数据库。

编辑:对不起,我没有上传我使用的代码:

Sub Insert2() Const cstrSheetName As String = "Weekly" Dim strDir As String Dim strFile As String Dim strTableName As String Dim MyPath As String Dim i As Long i = 0 MyPath = "C:\Products" strTableName = "Sells" If Left(MyPath, 1) <> "\" Then strDir = MyPath & "\" Else strDir = MyPath End If strFile = Dir(MyPath & "\Sells.XLSX") While strFile <> "" i = i + 1 strFile = strDir & strFile Debug.Print "importing " & strFile DoCmd.TransferSpreadsheet _ TransferType:=acImport, _ SpreadsheetType:=acSpreadsheetTypeExcel9, _ TableName:=strTableName, _ FileName:=strFile, _ HasFieldNames:=True, _ Range:=cstrSheetName & "$" strFile = Dir() Wend End Sub 

你认为你能帮助我吗?

非常感谢

 Dim FileSystem As Object Dim HostFolder As String HostFolder = MyPath Set FileSystem = CreateObject("Scripting.FileSystemObject") DoFolder FileSystem.GetFolder(HostFolder) Sub DoFolder(Folder) Dim SubFolder For Each SubFolder In Folder.SubFolders Dim File For Each File In Folder.Files ' Operate on each file Next Next End Sub 

将代码信誉编码为Rich,重新排列后的代码,以便它不recursion迭代所有子文件夹,只是MyPath的子文件夹