跳到主要内容

如何将多个文本文件导入到多个工作表?

假设计算机的文件夹中有多个文本文件,现在,您要将这些文本文件导入Excel工作簿,并将每个文本文件放入单独的工作表中。 将每个文本文件一张一张地复制并粘贴到工作表上会浪费很多时间,在这里,我可以为您介绍一些解决此问题的技巧。

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

使用 Kutools for Excel 将工作簿拆分为多个单独的文本/pdf/csv/xlsx 文件


箭头蓝色右气泡 使用VBA代码将多个文本文件导入到单独的工作表中

以下VBA代码可以帮助您立即将每个文本文件导入到新工作簿中的单独工作表中,请执行以下操作:

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

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

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

Sub CombineTextFiles()
'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 (*.txt), *.txt", , "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
    xWb.Worksheets(I).Columns("A:A").TextToColumns _
      Destination:=Range("A1"), DataType:=xlDelimited, _
      TextQualifier:=xlDoubleQuote, _
      ConsecutiveDelimiter:=False, _
      Tab:=False, Semicolon:=False, _
      Comma:=False, Space:=False, _
      Other:=True, OtherChar:="|"
    Do While I < UBound(xFilesToOpen)
        I = I + 1
        Set xTempWb = Workbooks.Open(xFilesToOpen(I))
        With xWb
            xTempWb.Sheets(1).Move after:=.Sheets(.Sheets.Count)
            .Worksheets(I).Columns("A:A").TextToColumns _
              Destination:=Range("A1"), DataType:=xlDelimited, _
              TextQualifier:=xlDoubleQuote, _
              ConsecutiveDelimiter:=False, _
              Tab:=False, Semicolon:=False, _
              Comma:=False, Space:=False, _
              Other:=True, OtherChar:=xDelimiter
        End With
    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 键以运行此代码,然后在弹出的窗口中指定包含文本文件的文件夹,转到该文件夹​​以选择要导入到工作表的文本文件,请参见屏幕截图:

doc导入多个文本文件1

4。 然后点击 可选 按钮,所有选定的文本文件都已导入到新工作簿中,每个文件分别位于一个工作表中。

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


箭头蓝色右气泡 使用 Kutools for Excel 将工作簿拆分为多个单独的文本/pdf/csv/xlsx 文件

有时,您可能需要执行与上述任务相反的操作,也就是说,您需要将工作簿拆分为单独的文本文件。 Kutools for Excel拆分工作簿 实用程序可以帮助您快速轻松地解决此工作。

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

安装后 Kutools for Excel,请执行以下操作: 立即免费下载Kutools for Excel! )

1。 激活您要拆分为多个文本文件的工作簿。

2。 点击 企业 > 工作簿 > 拆分工作簿,请参见屏幕截图:

3。 在 拆分工作簿 对话框:

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

(2.)检查 指定保存格式,然后选择要保存的文件格式,例如,可以选择 TXT, CSV, PDF格式, XLSX or XLS 所需的文件格式。

(3.)然后单击 分裂 按钮,在以下提示框中指定一个文件夹以输出单独的文件。

Excel加载项拆分工作簿对话框 2 excel加载项选择位置

4。 然后点击 OK 按钮,您选择的工作表将被拆分并以所需的格式保存到所需的文件夹中。

excel加载项单独的文件

立即免费下载Kutools for 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 (10)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
I had tried with a txt file where there were multiple tables with numbers saved with commas but the program did not recognize the comma as a decimal point. How can I fix this error?
This comment was minimized by the moderator on the site
What would I need to change in the code to make it not open them in a new workbook?
This comment was minimized by the moderator on the site
This code helped me exactly to solve my issue, thanks for the code.
This comment was minimized by the moderator on the site
thank you , multiple text files to different worksheets code worked!
This comment was minimized by the moderator on the site
Thanks for this code this is working great.. i want to merge all the selected sheet into a single sheet is that possible..
This comment was minimized by the moderator on the site
How do I change this macros to display text file saved as UTF-8 or in other words using Platform = 65001, Thanks
This comment was minimized by the moderator on the site
Thanks for this very useful code. I have a question though. What I can change to convert the datatype from General to Text as currently it only imports data into General format.
This comment was minimized by the moderator on the site
Is it possible to open Sequencially multiple txt files in a single sheet ?
This comment was minimized by the moderator on the site
The code for "Import multiple text files to separate worksheets with VBA code" works for me, BUT cuts off the data of each cell at 255 characters. I think it is defaulting to GeneralFormat cell data, but I need it to be TextFormat. Unfortunately I cannot figure out how to adjust the code to fix this.
This comment was minimized by the moderator on the site
I used Following code But Space Didn't Delimit. Turned Tab:=True, Space:=True, but it didn't work. My Txt file format is- ALUMINI16MAYFUT 09-05-2016 10:00:00 106.0000 106.0000 105.2000 105.3500 104 Any Solution?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations