跳到主要内容

如何将多个文件名导入Excel中的单元格?

假设您有一个包含数百个文件的文件夹,现在,您要将这些文件名导入工作表的单元格中。 一个接一个地复制和粘贴将花费大量时间,在本文中,我将讨论一些快速技巧,以帮助您将一个文件夹中的多个文件名导入到工作表中。

使用VBA代码将多个文件名导入工作表单元格

使用 Kutools for Excel 将多个文件名导入工作表单元格


使用VBA代码将多个文件名导入工作表单元格

以下VBA代码可以帮助您将文件名,文件扩展名和文件夹名称导入工作表单元格,请执行以下步骤:

1。 启动您要导入文件名的新工作表。

2。 按住 ALT + F11 键打开 Microsoft Visual Basic应用程序 窗口。

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 将多个文件名导入工作表单元格

如果您需要同时在文件夹和子文件夹中导入文件名,请不要担心,使用 Kutools for Excel 文件名列表 实用程序,您可以轻松地导入特定文件夹(包括子文件夹)中的所有文件名。

Kutools for Excel : 带有300多个便捷的Excel加载项,可以在30天内免费试用. 

安装后 Kutools for Excel,请执行以下操作:

1。 点击 Kutools 加> 进出口 > 文件名列表,请参见屏幕截图:

2。 在 文件名列表 对话框中,进行以下操作:

(1.)点击 doc导入文件名5 按钮选择包含要导入文件的文件夹;

(2.)检查 在子目录中包含文件 导入子文件夹的文件名;

(3.)在“文件名”下指定要导入的文件类型。 档案类型 部分;

(4.)从菜单中选择要显示的文件大小单位。 文件大小单位 部分;

(5.)如果要超链接文件名和文件夹,请检查 创建超链接 选项。

doc导入文件名4

3。 完成设置后,请点击 OK 按钮,文件夹和子文件夹中的文件名都被导入到新的工作表中,如以下屏幕截图所示:

doc导入文件名6

Tips:如果只想从特定文件夹导入一种指定类型的文件名,则可以检查 指定 在选项 文件名列表 对话框,然后在其中输入文件扩展名,那么它将仅从文件夹和子文件夹中导入指定类型的文件名。

单击以了解有关此文件名列表功能的更多信息…

立即下载和免费试用Excel的Kutools!


演示:使用 Kutools for Excel 将多个文件名导入工作表单元格

Kutools for Excel:具有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 Tab 为 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