跳到主要内容

如何将多个csv文件组合/导入到多个工作表中?

如果要从一个文件夹中将多个csv文件作为工作簿中的单独工作表导入,如何在Excel中处理此工作?

使用VBA代码将多个csv文件导入到单独的工作表中

使用 Kutools for Excel 将多个 csv 文件导入单独的工作表

将工作簿拆分为多个单独的csv / pdf / txt / excel文件


要将多个csv文件快速导入到工作簿中,以下VBA代码可能会有所帮助,请执行以下操作:

1。 按住 ALT + F11 在Excel中打开键 Microsoft Visual Basic应用程序 窗口。

2。 点击 插页 > 模块,然后将以下代码粘贴到 模块 窗口。

VBA代码:将多个文本文件导入到单独的工作表中:

Sub CombineCsvFiles()
'updateby Extendoffice
    Dim xFilesToOpen As Variant
    Dim I As Integer
    Dim xWb As Workbook
    Dim xTempWb As Workbook
    Dim xDelimiter As String
    Dim xScreen As Boolean
    On Error GoTo ErrHandler
    xScreen = Application.ScreenUpdating
    Application.ScreenUpdating = False
    xDelimiter = "|"
    xFilesToOpen = Application.GetOpenFilename("Text Files (*.csv), *.csv", , "Kutools for Excel", , True)
    If TypeName(xFilesToOpen) = "Boolean" Then
        MsgBox "No files were selected", , "Kutools for Excel"
        GoTo ExitHandler
    End If
    I = 1
    Set xTempWb = Workbooks.Open(xFilesToOpen(I))
    xTempWb.Sheets(1).Copy
    Set xWb = Application.ActiveWorkbook
    xTempWb.Close False
    Do While I < UBound(xFilesToOpen)
        I = I + 1
        Set xTempWb = Workbooks.Open(xFilesToOpen(I))
        xTempWb.Sheets(1).Move , xWb.Sheets(xWb.Sheets.Count)
    Loop
ExitHandler:
    Application.ScreenUpdating = xScreen
    Set xWb = Nothing
    Set xTempWb = Nothing
    Exit Sub
ErrHandler:
    MsgBox Err.Description, , "Kutools for Excel"
    Resume ExitHandler
End Sub

3。 然后按 F5 键执行此代码,然后会弹出一个窗口,转到特定文件夹,然后选择要导入到Excel的csv文件,请参见屏幕截图:

doc导入csv到工作表1

4。 然后点击 可选 按钮,所选的csv文件已作为新工作簿中的单独工作表导入。

5。 最后,您可以根据需要保存新工作簿。


如果你有 Kutools for Excel,其功能强大的工具-结合,您可以根据需要快速将多个csv文件组合或导入到一个工作表或多个分离的工作表中。

安装后 Kutools for Excel,请这样做:

1。 请点击 Kutools 加 > 结合,请参见屏幕截图:

2.合并工作表 对话框,请选择 将工作簿中的多个工作表合并到一个工作簿中,请参见屏幕截图:

doc导入csv到工作表6

3。 在 向导中,单击 地址 > 文件 选择要合并的csv文件,请参见屏幕截图:

doc导入csv到工作表7

4。 在弹出 可选 对话框中选择 (* .csv) 从下拉列表中,然后指定要合并的csv文件。

doc导入csv到工作表8

5。 然后点击 可选 按钮,您选择的csv文件已导入对话框,请参见屏幕截图:

doc导入csv到工作表9

6。 在 ,请执行以下操作:

(1.)检查 每个工作表的第一行(评论) 来自 插入工作表信息 部分,所有csv文件信息将作为注释插入到合并的工作表中;

(2.)您可以根据需要重命名csv文件;

(3.)遇到空白csv文件时,可以跳过空白的csv文件。

doc导入csv到工作表10

7。 然后点击 完成 按钮,完成合并后,您可以为新的合并工作簿指定文件名和位置。 看截图:

doc导入csv到工作表11

8。 然后点击 保存 按钮,在弹出的promt框中,您可以打开新文件,然后单击 USB MIDI(XNUMX通道),然后点击关闭 没有,请参见屏幕截图:

doc导入csv到工作表12

9。 然后您可以随意保存此方案。 看截图:

doc导入csv到工作表13

10。 最后,您选择的csv文件已被导入到具有单独工作表的新工作簿中,请参见屏幕截图:

doc导入csv到工作表14

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


Kutools for Excel:具有300多个方便的Excel加载项,可以在30天内免费试用,没有任何限制。 立即下载并免费试用!

在某些情况下,您可能想将工作簿拆分为单独的 csv 文件,Kutools for Excel 的 拆分工作簿实用程序可以快速轻松地完成这项工作。

安装后 Kutools for Excel,请这样做:

1。 点击 Kutools 加 > 工作簿 > 拆分工作簿,请参见屏幕截图:

2.拆分工作簿 对话框:

(1.)检查要拆分的工作表;

(2.)并检查 指定保存格式,然后选择要保存的文件格式;

(3.)然后单击 分裂 按钮,然后指定一个文件夹来放置分开的文件。

doc导入csv到工作表3

3。 然后点击 OK 要开始拆分,工作簿将拆分为所需的单独的csv文件。

doc导入csv到工作表4

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


Kutools for Excel:具有300多个方便的Excel加载项,可以在30天内免费试用,没有任何限制。 立即下载并免费试用!

最佳办公生产力工具

🤖 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 (7)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi!
I've been using this code to combine evaluation forms into one excel sheet. However, it reads the dates in the US format of MM/DD/YYYY instead of the European format which I use, which is DD/MM/YYYY. The results is a column that looks like this:

11-4-2021 17:13
22/10/2021 09:41:32 (This is october 22nd, but it's not recognized)
7-12-2022 14:55 (This is mistakenly read as December 7th, when it should be July 12th.)
27/10/2021 16:53:19
29/10/2021 09:44:26
11-1-2021 11:36
11-2-2021 14:11
17/08/2022 16:46:06
11-1-2021 10:49
8-10-2022 12:19
22/10/2021 14:54:58
17/11/2021 13:48:54
26/10/2021 20:13:37
22/10/2021 16:26:13

How can I prevent this from happening? I saw a comment on another post that suggested putting 'Local=True' somewhere, but I've not been able to figure out where to put that so that the CSV files load correctly. Any help is appreciated!
This comment was minimized by the moderator on the site
Hello this vba macro is great, but i want it to be launched by a button click on another workbook, how can i do that ?
This comment was minimized by the moderator on the site
Thank you. The code works well
This comment was minimized by the moderator on the site
Thank you . Very handy
This comment was minimized by the moderator on the site
Thank you! The code was very useful
This comment was minimized by the moderator on the site
Thank you for sharing this code. It is exactly what I was looking for. Sadly it is not working as well as I hoped it would. I am receiving an error message every time the code is run. ERROR: "The name is already taken. Try a different one." Notes: - I am trying to combine four csv files - All of the csv files have the same name "file_name.csv, file_name(1).csv, etc..." The code combines the first two csv files into one workbook and the the third file into its own workbook. The fourth file never had a chance because of the error. If anyone has some thoughts as how to go about fixing this issue I would greatly appreciate your feedback. Thank you all in advanced for you time. Best regards, Andrew
This comment was minimized by the moderator on the site
Thank you for the code on combining files. I however want to have the multiple files in the workbook I am currently working with, not for the code to create a new workbook. How do I go about achieving that?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations