跳到主要內容

如何將多個文本文件導入到多個工作表?

假設計算機的文件夾中有多個文本文件,現在,您要將這些文本文件導入Excel工作簿,並將每個文本文件放入單獨的工作表中。 將每個文本文件一張一張地複制並粘貼到工作表上會浪費很多時間,在這裡,我可以為您介紹一些解決此問題的技巧。

使用VBA代碼將多個文本文件導入到單獨的工作表中

使用Kutools for Excel將工作簿拆分為多個單獨的文本/ pdf / csv / xlsx文件


箭頭藍色右氣泡 使用VBA代碼將多個文本文件導入到單獨的工作表中

以下VBA代碼可以幫助您立即將每個文本文件導入到新工作簿中的單獨工作表中,請執行以下操作:

1。 按住 ALT + F11 在Excel中打開鍵 Microsoft Visual Basic for Applications 窗口。

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文件

有時,您可能需要執行與上述任務相反的操作,也就是說,您需要將工作簿拆分為單獨的文本文件。 Excel的Kutools拆分工作簿 實用程序可以幫助您快速輕鬆地解決此工作。

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

安裝後 Excel的Kutools,請執行以下操作: 立即免費下載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選項卡為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