跳到主要內容

如何將多個文件名導入Excel中的單元格?

假設您有一個包含數百個文件的文件夾,現在,您要將這些文件名導入工作表的單元格中。 一個接一個地複制和粘貼將花費大量時間,在本文中,我將討論一些快速技巧,以幫助您將一個文件夾中的多個文件名導入到工作表中。

使用VBA代碼將多個文件名導入工作表單元格

使用Kutools for Excel將多個文件名導入工作表單元格


使用VBA代碼將多個文件名導入工作表單元格

以下VBA代碼可以幫助您將文件名,文件擴展名和文件夾名稱導入工作表單元格,請執行以下步驟:

1。 啟動您要導入文件名的新工作表。

2。 按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications 窗口。

3。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊 窗口。

VBA代碼:將多個文件名導入工作表的單元格

Sub GetFileList()
'updateby Extendoffice
    Dim xFSO As Object
    Dim xFolder As Object
    Dim xFile As Object
    Dim xFiDialog As FileDialog
    Dim xPath As String
    Dim i As Integer
    Set xFiDialog = Application.FileDialog(msoFileDialogFolderPicker)
    If xFiDialog.Show = -1 Then
        xPath = xFiDialog.SelectedItems(1)
    End If
    Set xFiDialog = Nothing
    If xPath = "" Then Exit Sub
    Set xFSO = CreateObject("Scripting.FileSystemObject")
    Set xFolder = xFSO.GetFolder(xPath)
    ActiveSheet.Cells(1, 1) = "Folder name"
    ActiveSheet.Cells(1, 2) = "File name"
    ActiveSheet.Cells(1, 3) = "File extension"
    i = 1
    For Each xFile In xFolder.Files
        i = i + 1
        ActiveSheet.Cells(i, 1) = xPath
        ActiveSheet.Cells(i, 2) = Left(xFile.Name, InStrRev(xFile.Name, ".") - 1)
        ActiveSheet.Cells(i, 3) = Mid(xFile.Name, InStrRev(xFile.Name, ".") + 1)
    Next
End Sub

4. 然後按 F5 鍵執行此代碼,然後在彈出的“瀏覽”窗口中,選擇要導入文件名形式的文件夾,請參見屏幕截圖:

doc導入文件名1

5。 然後點擊 OK 按鈕,您將得到以下結果:

doc導入文件名2

備註:如果您的特定文件夾中有子文件夾,則不會導入子文件夾中的文件名。


使用Kutools for Excel將多個文件名導入工作表單元格

如果您需要同時在文件夾和子文件夾中導入文件名,請不要擔心,使用 Excel的Kutools 文件名列表 實用程序,您可以輕鬆地導入特定文件夾(包括子文件夾)中的所有文件名。

Excel的Kutools : 帶有300多個便捷的Excel加載項,可以在30天內免費試用. 

安裝後 Excel的Kutools,請執行以下操作:

1。 點擊 Kutools 加> 進出口 > 文件名列表,請參見屏幕截圖:

2。 在 文件名列表 對話框中,進行以下操作:

(1.)點擊 doc導入文件名5 按鈕選擇包含要導入文件的文件夾;

(2.)檢查 在子目錄中包含文件 導入子文件夾的文件名;

(3.)在“文件名”下指定要導入的文件類型。 檔案類型 部分;

(4.)從菜單中選擇要顯示的文件大小單位。 文件大小單位 部分;

(5.)如果要超鏈接文件名和文件夾,請檢查 創建超鏈接 選項。

doc導入文件名4

3。 完成設置後,請點擊 OK 按鈕,文件夾和子文件夾中的文件名都被導入到新的工作表中,如以下屏幕截圖所示:

doc導入文件名6

保養竅門:如果只想從特定文件夾導入一種指定類型的文件名,則可以檢查 指定 在選項 文件名列表 對話框,然後在其中輸入文件擴展名,那麼它將僅從文件夾和子文件夾中導入指定類型的文件名。

單擊以了解有關此文件名列表功能的更多信息…

立即下載和免費試用Excel的Kutools!


演示:使用Kutools for Excel將多個文件名導入工作表單元格

Excel的Kutools:具有300多個方便的Excel加載項,可以在30天內免費試用,沒有任何限制。 立即下載並免費試用!

相關文章:

如何將文件夾和子文件夾中的所有文件列出到工作表中?

如何列出文件夾中的所有文件名並在Excel中為其創建超鏈接?

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (14)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
very helpful. Tnx much
Rated 5 out of 5
This comment was minimized by the moderator on the site
Another easy method is there. Just copy the directory path where you have saved the documents, and paste that link in browser (chrome/mozilla). You will get the name list and just copy paste into excel. Cheers!
This comment was minimized by the moderator on the site
i want to add table,only folder name,and data modified as well?
This comment was minimized by the moderator on the site
Hello,
Do you want to list all folder names from a specific main folder? Please give your problem more detailed.
This comment was minimized by the moderator on the site
i want to mention only folder name.what is vba code for that.
This comment was minimized by the moderator on the site
is there a way to put the folder path already in the code?
This comment was minimized by the moderator on the site
For the VBA code, how would you pull in the date modified as well?
This comment was minimized by the moderator on the site
Hello,
Add the date modified column for the imported filenames, please apply the following VBA code, please try it, hope it can help you!

Sub GetFileList()
Dim xFSO As Object
Dim xFolder As Object
Dim xFile As Object
Dim xFiDialog As FileDialog
Dim xPath As String
Dim i As Integer
Set xFiDialog = Application.FileDialog(msoFileDialogFolderPicker)
If xFiDialog.Show = -1 Then
xPath = xFiDialog.SelectedItems(1)
End If
Set xFiDialog = Nothing
If xPath = "" Then Exit Sub
Set xFSO = CreateObject("Scripting.FileSystemObject")
Set xFolder = xFSO.GetFolder(xPath)
ActiveSheet.Cells(1, 1) = "Folder name"
ActiveSheet.Cells(1, 2) = "File name"
ActiveSheet.Cells(1, 3) = "File extension"
ActiveSheet.Cells(1, 4) = "Date last modified"
i = 1
For Each xFile In xFolder.Files
i = i + 1
ActiveSheet.Cells(i, 1) = xPath
ActiveSheet.Cells(i, 2) = Left(xFile.Name, InStrRev(xFile.Name, ".") - 1)
ActiveSheet.Cells(i, 3) = Mid(xFile.Name, InStrRev(xFile.Name, ".") + 1)
ActiveSheet.Cells(i, 4) = CDate(xFile.datelastmodified)
Next
End Sub
This comment was minimized by the moderator on the site
Thanks. It was awesome
This comment was minimized by the moderator on the site
saya coba yg pake vba di run bisa tapi pas di step browser nya bukan oke tapi open terus sampai ke file tertentu dan ketika di klik malah run time 26 path not found bisa bantu kenapa itu bisa terjadi?
This comment was minimized by the moderator on the site
Apakah bisa merename file dengan excel?
terimakasih sangat membantu perkerjaan saya,
This comment was minimized by the moderator on the site
Very helpful, thank you!
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations