MS Excel VBA-获取已声明文件夹的子文件夹中的文件名
我试图循环遍历文件夹中的所有子文件夹,编译所有文件的名称。不过我有点麻烦,因为我从一个宏改编了这段代码,该宏只循环遍历一个文件夹并编译文件名。非常感谢您抽出时间MS Excel VBA-获取已声明文件夹的子文件夹中的文件名,vba,excel,Vba,Excel,我试图循环遍历文件夹中的所有子文件夹,编译所有文件的名称。不过我有点麻烦,因为我从一个宏改编了这段代码,该宏只循环遍历一个文件夹并编译文件名。非常感谢您抽出时间 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) >
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在以下站点找到了答案:
希望能给你一些帮助…非常人性化 在你发布链接之前,我正在编写这段代码,因此我重新考虑发布我的工作。所以我查看了你发布的链接。我相信我想发布的代码是简洁的&更好地解决了这一点。所以你来了 第一步。参考文献 第二步。密码
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