跳到主要內容

如何將多個csv文件組合/導入到多個工作表中?

如果要從一個文件夾中將多個csv文件作為工作簿中的單獨工作表導入,如何在Excel中處理此工作?

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

使用Kutools for Excel將多個csv文件導入到單獨的工作表中

將工作簿拆分為多個單獨的csv / pdf / txt / excel文件


要將多個csv文件快速導入到工作簿中,以下VBA代碼可能會有所幫助,請執行以下操作:

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

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。 最後,您可以根據需要保存新工作簿。


如果你有 Excel的Kutools,其功能強大的工具-結合,您可以根據需要快速將多個csv文件組合或導入到一個工作表或多個分離的工作表中。

安裝後 Excel的Kutools,請這樣做:

1。 請點擊 Kutools 加 > 結合,請參見屏幕截圖:

2.合併工作表 對話框,請選擇 將工作簿中的多個工作表合併到一個工作簿中,請參見屏幕截圖:

doc導入csv到工作表6

3。 在 步驟 2 嚮導中,單擊 加入 > 文件 選擇要合併的csv文件,請參見屏幕截圖:

doc導入csv到工作表7

4。 在彈出 已提交 對話框中選擇 (* .csv) 從下拉列表中,然後指定要合併的csv文件。

doc導入csv到工作表8

5。 然後點擊 已提交 按鈕,您選擇的csv文件已導入對話框,請參見屏幕截圖:

doc導入csv到工作表9

6。 在 步驟 3,請執行以下操作:

(1.)檢查 每個工作表的第一行(評論) 來自 插入工作表信息 部分,所有csv文件信息將作為註釋插入到合併的工作表中;

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

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

doc導入csv到工作表10

7。 然後點擊 按鈕,完成合併後,您可以為新的合併工作簿指定文件名和位置。 看截圖:

doc導入csv到工作表11

8。 然後點擊 節省 按鈕,在彈出的promt框中,您可以打開新文件,然後單擊 ,然後通過單擊將其關閉 沒有,請參見屏幕截圖:

doc導入csv到工作表12

9。 然後,您可以根據需要保存或不保存此方案。 看截圖:

doc導入csv到工作表13

10。 最後,您選擇的csv文件已被導入到具有單獨工作表的新工作簿中,請參見屏幕截圖:

doc導入csv到工作表14

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


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

在某些情況下,您可能需要將工作簿拆分為單獨的csv文件,Kutools for Excel的“拆分工作簿”實用工具可以快速輕鬆地完成此工作。

安裝後 Excel的Kutools,請這樣做:

1。 點擊 Kutools 加 > 工作簿 > 拆分工作簿,請參見屏幕截圖:

2.拆分工作簿 對話框:

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

(2.)並檢查 指定保存格式,然後選擇要保存的文件格式;

(3.)然後單擊 分裂 按鈕,然後指定一個文件夾來放置分隔的文件。

doc導入csv到工作表3

3。 然後點擊 OK 要開始拆分,工作簿將拆分為所需的單獨的csv文件。

doc導入csv到工作表4

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


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

最佳辦公生產力工具

🤖 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 (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