如何在Excel中列出所有文件夾和子文件夾?
您是否曾經遇到過將指定目錄中的所有文件夾和子文件夾都列出到工作表中的問題? 在Excel中,沒有一種快速簡便的方法可以一次獲取特定目錄中所有文件夾的名稱。 為了處理該任務,本文可能會對您有所幫助。
列出所有帶有VBA代碼的文件夾和子文件夾
如果要從指定目錄中獲取所有文件夾名稱,則以下VBA代碼可能會對您有所幫助,請這樣做:
1。 按住 ALT + F11 鍵,然後打開 Microsoft Visual Basic for Applications窗口.
2。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊窗口.
VBA代碼:列出所有文件夾和子文件夾名稱
Sub FolderNames()
'Update 20141027
Application.ScreenUpdating = False
Dim xPath As String
Dim xWs As Worksheet
Dim fso As Object, j As Long, folder1 As Object
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Choose the folder"
.Show
End With
On Error Resume Next
xPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1) & "\"
Application.Workbooks.Add
Set xWs = Application.ActiveSheet
xWs.Cells(1, 1).Value = xPath
xWs.Cells(2, 1).Resize(1, 5).Value = Array("Path", "Dir", "Name", "Date Created", "Date Last Modified")
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder1 = fso.getFolder(xPath)
getSubFolder folder1
xWs.Cells(2, 1).Resize(1, 5).Interior.Color = 65535
xWs.Cells(2, 1).Resize(1, 5).EntireColumn.AutoFit
Application.ScreenUpdating = True
End Sub
Sub getSubFolder(ByRef prntfld As Object)
Dim SubFolder As Object
Dim subfld As Object
Dim xRow As Long
For Each SubFolder In prntfld.SubFolders
xRow = Range("A1").End(xlDown).Row + 1
Cells(xRow, 1).Resize(1, 5).Value = Array(SubFolder.Path, Left(SubFolder.Path, InStrRev(SubFolder.Path, "\")), SubFolder.Name, SubFolder.DateCreated, SubFolder.DateLastModified)
Next SubFolder
For Each subfld In prntfld.SubFolders
getSubFolder subfld
Next subfld
End Sub
3。 然後按 F5 運行此代碼的關鍵,以及 選擇文件夾 窗口將彈出,然後您需要選擇要列出文件夾和子文件夾名稱的目錄,請參見屏幕截圖:
4。 點擊 OK,您將在新工作簿中獲得文件夾和子文件夾的路徑,目錄,名稱,創建日期和上次修改日期,請參見屏幕截圖:
相關文章:
最佳辦公生產力工具
熱門特色: 尋找、突出顯示或識別重複項 | 刪除空白行 | 合併列或儲存格而不遺失數據 | 沒有公式的回合 ... | |
超級查詢: 多條件VLookup | 多值VLookup | 跨多個工作表的 VLookup | 模糊查詢 .... | |
高級下拉列表: 快速建立下拉列表 | 依賴下拉列表 | 多選下拉列表 .... | |
欄目經理: 新增特定數量的列 | 移動列 | 切換隱藏列的可見性狀態 | 比較範圍和列 ... | |
特色功能: 網格焦點 | 設計圖 | 大方程式酒吧 | 工作簿和工作表管理器 | 資源庫 (自動文字) | 日期選擇器 | 合併工作表 | 加密/解密單元格 | 按清單發送電子郵件 | 超級濾鏡 | 特殊過濾器 (過濾粗體/斜體/刪除線...)... | |
前 15 個工具集: 12 文本 工具 (添加文本, 刪除字符,...) | 50+ 圖表 類型 (甘特圖,...) | 40+ 實用 公式 (根據生日計算年齡,...) | 19 插入 工具 (插入二維碼, 從路徑插入圖片,...) | 12 轉化 工具 (數字到單詞, 貨幣兌換,...) | 7 合併與拆分 工具 (高級合併行, 分裂細胞,...) | ... 和更多 |
增強您的 Excel 技能 Kutools for Excel,體驗前所未有的效率。 Kutools for Excel 提供 300 多種高級功能,可提高工作效率並節省時間。 點擊此處獲取您最需要的功能...
Office Tab 為 Office 帶來選項卡式界面,讓您的工作更輕鬆
- 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
- 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
- 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!

Sort comments by

#36267
This comment was minimized by the moderator on the site
Report
0
0

#35209
This comment was minimized by the moderator on the site
Report
0
0

#34381
This comment was minimized by the moderator on the site
0
0

#32810
This comment was minimized by the moderator on the site
0
0

#32124
This comment was minimized by the moderator on the site
0
0

#31674
This comment was minimized by the moderator on the site
0
0

#29285
This comment was minimized by the moderator on the site
0
0

#26983
This comment was minimized by the moderator on the site
0
0

#22802
This comment was minimized by the moderator on the site
0
0

#22803
This comment was minimized by the moderator on the site
0
0

#20750
This comment was minimized by the moderator on the site
0
0
There are no comments posted here yet