2015-06-24 156 views
0

我想打印特定文件夾下所有excel文件的列表。除文件名外,我還應該能夠打印excel文件下所有表格的名稱。Excel VBA列出特定文件中的所有工作表

下面是我打印文件名的代碼示例如下。但是,我無法弄清楚如何閱讀該文件下的圖紙名稱列表?

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 

感謝幫助。由於

+0

如果需要滿足所有片材類型(包括圖表工作表),你必須通過打開工作簿和循環牀單。 – Rory

+0

使用[this](http://stackoverflow.com/questions/14358443/vba-list-of-sheets-hyperlinked)或[this](http://stackoverflow.com/questions/10654797/how-can- i-use-vba-list-all-worksheets-in-a-workbook-in-a-string)回答並將其包裝在「Workbook.Open」和「.Close」命令中。 – nhee

+0

如果您不想打開每個Excel工作簿以獲取工作表名稱,則可以使用ADO並查詢已關閉的工作簿。例如[這裏](http://www.mrexcel.com/forum/excel-questions/47074-get-worksheet-names-closed-workbook.html)。 – dee

回答

2

你必須打開通過片狀物體的工作簿和循環

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 
相關問題