简体   繁体   English

Excel VBA列出特定文件中的所有工作表

[英]Excel VBA list all sheets in a specific file

I am trying to print a list of all the excel files under a specific folder. 我正在尝试在特定文件夹下打印所有Excel文件的列表。 Along with the file names, I should also be able to print the names of all the sheets under the excel file. 除了文件名,我还应该能够在excel文件下打印所有工作表的名称。

Below is a sample of my code for printing the file names is as below. 下面是我用于打印文件名的代码示例,如下所示。 However, I am not able to figure out how to read the list of sheet names under the file? 但是,我不知道如何读取文件下的工作表名称列表?

    Sub ButtonRun_Excel_Compare_Report_Click()
    Sheet1.Cells.Clear
    ShowFolderList ("D:\temp")
End Sub

Sub ShowFolderList(folderspec)
    Dim fs, f, f1, fc, s, sFldr
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(folderspec)
    Set fc = f.SubFolders
     For Each f1 In fc
        If Right(f1, 1) <> "\" Then ShowFolderList f1 & "\" Else ShowFolderList f1
    Next
    Set fc = f.Files
    Rowcnt = 1
    For Each f1 In fc

        If f1.Name Like "*.xls" Then

            'How to loop through the Sheets under exach file?

            Sheet1.Cells(Rowcnt, 1) = f1.Name
            'Sheet1.Cells(Rowcnt, 2) = f1.Path
            Rowcnt = Rowcnt + 1
            'Debug.Print folderspec & f1.Name
        End If
    Next
End Sub

Appreciate the help. 感谢帮助。 Thanks 谢谢

You'll have to open the workbook and loop through the sheet objects 您必须打开工作簿并遍历图纸对象

Application.Workbooks.Open Filename:=folderspec & f1.Name
For i = 1 to ActiveWorkbook.Sheets.Count
    strFlenme = ActiveWorkbook.FullName
    strShtNme = ActiveWorkbook.Sheets(i).Name
    Debug.print strFlenme & " - " & i & " '" & strShtNme & "'" 
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM