我一直在尝试编写一个宏,列出我目录中每个工作簿的工作表名称。这些文件存储在各种文件夹和子文件夹中,所以我希望它可以提取所有内容,我可以从整个输出中推断出我需要的信息。这段代码是我在本网站用户的帮助下整理的,但我仍未得到理想的结果。有人请帮忙!
Sub machinegun()
FileType = "*.xls*"
OutputRow = 2
Set fso = CreateObject("Scripting.FileSystemObject")
Set Folder = fso.GetFolder("G:")
Set Curr_File = (filepath & FileType)
For Each Subfolder In Folder.Subfolders
Do Until Curr_File = ""
Set Fldr.wkbk = Workbooks.Open(filepath & Curr_File, False, True)
ThisWorkbook.ActiveSheet.Range("A" & OutputRow) = Curr_File
ThisWorkbook.ActiveSheet.Range("B" & OutputRow).ClearContents
OutputRow = OutputRow + 1
For Each Sht In FldrWkbk.Sheets
ThisWorkbook.ActiveSheet.Range("B" & OutputRow) = Sht.Name
ThisWorkbook.ActiveSheet.Range("A" & OutputRow).ClearContents
OutputRow = OutputRow + 1
Next Sht
FldrWkbk.Close SaveChanges:=False
Loop
Next
End Sub
答案 0 :(得分:1)
以下是来自Loop Through All Subfolders Using VBA的内容,适用于您:
Option Explicit
Sub LoopThroughFiles()
Dim FileSystem As Object
Dim HostFolder As String
HostFolder = "C:\Users\"
Set FileSystem = CreateObject("Scripting.FileSystemObject")
DoFolder FileSystem.GetFolder(HostFolder)
End Sub
Sub DoFolder(Folder)
Dim SubFolder
For Each SubFolder In Folder.SubFolders
DoFolder SubFolder
Next
Dim File
For Each File In Folder.Files
Debug.Print File.name
Next
End Sub
只需确保您运行LoopThroughFiles()
并将HostFolder
更改为有意义的内容。然后使用File.name
中的debug.print
。