我正在尝试遍历文件夹中的所有子文件夹,编译文件的所有名称。我遇到了一些麻烦,因为我从一个只调整一个文件夹并编译文件名的宏调整了这个代码。非常感谢你的时间!
Sub FindFiles()
Cells(1, 1).Select
Dim F As String
Dim G As String
F = Dir("C:\Users\z003nttv\Desktop\Folder\" & "*")
Do While Len(F) > 0
Do While Len(G) > 0
G = Dir(F & "*.*")
ActiveCell.Formula = G
ActiveCell.Offset(1, 0).Select
G = Dir()
Loop
F = Dir()
Loop
End Sub
答案 0 :(得分:1)
在以下网站找到答案:
https://www.extendoffice.com/documents/excel/2994-excel-list-all-files-in-folder-and-subfolders.html
希望它可以提供一些帮助..非常用户友好!
答案 1 :(得分:1)
在您发布链接之前,我正在处理此代码,并且第二个想法是发布我的工作。所以我查看了你发布的链接。我相信我想发布的代码是简洁的&更好地解决了这一点。因此,你去吧
步骤1.参考
第二步。守则。
Sub FindFiles()
Dim fso As FileSystemObject
Dim Folder As Folder
Dim Files As Files
Dim path, s As String
'' Input Path : Please change as needed
path = Sheets("Sheet1").Cells(1, 2).Value
Set fso = New FileSystemObject
Set Basefolder = fso.GetFolder(path)
Set SubFolders = Basefolder.SubFolders
Dim i As Integer
i = 1
For Each Folder In SubFolders
Set Files = Folder.Files
For Each File In Files
With Sheets("Sheet1")
.Cells(i, 1).Value = Folder
.Cells(i, 2).Value = File
i = i + 1
Next File
Next Folder
End Sub