Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or
0
0
0
s2smodern

How to quickly batch import multiple csv/text/xml files in Excel?

In Excel, you may have tied to save a workbook as csv file, text file or xml file, but have you ever tried to import multiple csv/text/xml files from a folder into a workbook or a worksheet? In this article, I introduce some methods to quickly batch import them.

Import multiple text files from a folder into each worksheets of a workbook with VBA

Import multiple csv files from a folder into a single sheet with VBA

Import multiple xml files from a folder into a single sheet with VBA

Import or combine multiple xml/csv files into a sheet or a workbook with Kutools for Excel good idea3

Export each sheet as csv/text/pdf to a folder with Kutools for Excelgood idea3


Easily Combine multiple sheets/Workbook into one Single sheet or Workbook

To combinne multiples sheets or workbooks into one sheet or workbook may be edious in Excel, but with the Combine function in Kutools for Excel, you can combine merge dozens of sheets/workbooks into one sheet or workbook, also, you can consolidate the sheets into one by several clicks only.  Click for 60 days free trial!
combine sheets
 
Kutools for Excel: with more than 200 handy Excel add-ins, free to try with no limitation in 60 days.


To import text files from a folder to a workbook, you can use below VBA to quickly handle it.

1. Enable a blank workbook, and press Alt + F11 keys to open Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the VBA to the Module window.

VBA: Import all text files from a folder into a workbook

Sub LoadPipeDelimitedFiles()
'UpdatebyKutoolsforExcel20151214
    Dim xStrPath As String
    Dim xFileDialog As FileDialog
    Dim xFile As String
    Dim xCount As Long
    On Error GoTo ErrHandler
    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    xFileDialog.AllowMultiSelect = False
    xFileDialog.Title = "Select a folder [Kutools for Excel]"
    If xFileDialog.Show = -1 Then
        xStrPath = xFileDialog.SelectedItems(1)
    End If
    If xStrPath = "" Then Exit Sub
    Application.ScreenUpdating = False
    xFile = Dir(xStrPath & "\*.txt")
    Do While xFile <> ""
        xCount = xCount + 1
        Sheets(xCount).Select
        With ActiveSheet.QueryTables.Add(Connection:="TEXT;" _
          & xStrPath & "\" & xFile, Destination:=Range("A1"))
            .Name = "a" & xCount
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileOtherDelimiter = "|"
            .TextFileColumnDataTypes = Array(1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
            xFile = Dir
        End With
    Loop
    Application.ScreenUpdating = True
    Exit Sub
ErrHandler:
    MsgBox "no files txt", , "Kutools for Excel"
End Sub

3. Press F5 key or Run button to run the VBA, and select a folder that you want to import the text files from in the popping dialog. See screenshot:

doc import multiple csv text xml 1

4. And click OK, and each text file in the selected folder has been imported into one worksheet of the active workbook. See screenshot:

doc import multiple csv text xml 2doc import multiple csv text xml 3

To import all the csv files form a folder into a single sheet, you can use below VBA code.

1. Enable a blank worksheet, and press Alt + F11 keys to open Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste below VBA into the new Module window.

VBA: Import csv files from a folder into one worksheet

Sub ImportCSVsWithReference()
'UpdatebyKutoolsforExcel20151214
    Dim xSht  As Worksheet
    Dim xWb As Workbook
    Dim xStrPath As String
    Dim xFileDialog As FileDialog
    Dim xFile As String
    On Error GoTo ErrHandler
    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    xFileDialog.AllowMultiSelect = False
    xFileDialog.Title = "Select a folder [Kutools for Excel]"
    If xFileDialog.Show = -1 Then
        xStrPath = xFileDialog.SelectedItems(1)
    End If
    If xStrPath = "" Then Exit Sub
    Set xSht = ThisWorkbook.ActiveSheet
    If MsgBox("Clear the existing sheet before importing?", vbYesNo, "Kutools for Excel") = vbYes Then xSht.UsedRange.Clear
    Application.ScreenUpdating = False
    xFile = Dir(xStrPath & "\" & "*.csv")
    Do While xFile <> ""
        Set xWb = Workbooks.Open(xStrPath & "\" & xFile)
        Columns(1).Insert xlShiftToRight
        Columns(1).SpecialCells(xlBlanks).Value = ActiveSheet.Name
        ActiveSheet.UsedRange.Copy xSht.Range("A" & Rows.Count).End(xlUp).Offset(1)
        xWb.Close False
        xFile = Dir
    Loop
    Application.ScreenUpdating = True
    Exit Sub
ErrHandler:
    MsgBox "no files csv", , "Kutools for Excel"
End Sub

3. Press F5 key or click Run button to execute the VBA, and a dialog pops out to select a folder that you want to import all csv files from. See screenshot:

doc import multiple csv text xml 4

4. Click OK, and a dialog pops out to remind you if clear the contents of active worksheet before importing, here I click Yes. See screenshot:

doc import multiple csv text xml 5

After clicking Yes, all csv files in the selected folder are imported into the current sheet, and place data from Column A to right. See screenshot:

doc import multiple csv text xml 6doc import multiple csv text xml 7

Tip: If you want to place csv files horizontally in a worksheet, you can use below VBA.

Sub ImportCSVsWithReferenceI()
'UpdatebyKutoolsforExcel20151214
    Dim xSht  As Worksheet
    Dim xWb As Workbook
    Dim xStrPath As String
    Dim xFileDialog As FileDialog
    Dim xFile As String
    Dim xCount As Long
    On Error GoTo ErrHandler
    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    xFileDialog.AllowMultiSelect = False
    xFileDialog.Title = "Select a folder [Kutools for Excel]"
    If xFileDialog.Show = -1 Then
        xStrPath = xFileDialog.SelectedItems(1)
    End If
    If xStrPath = "" Then Exit Sub
    Set xSht = ThisWorkbook.ActiveSheet
    If MsgBox("Clear the existing sheet before importing?", vbYesNo, "Kutools for Excel") = vbYes Then
        xSht.UsedRange.Clear
        xCount = 1
    Else
        xCount = xSht.Cells(3, Columns.Count).End(xlToLeft).Column + 1
    End If
    Application.ScreenUpdating = False
    xFile = Dir(xStrPath & "\" & "*.csv")
    Do While xFile <> ""
        Set xWb = Workbooks.Open(xStrPath & "\" & xFile)
        Rows(1).Insert xlShiftDown
        Range("A1") = ActiveSheet.Name
        ActiveSheet.UsedRange.Copy xSht.Cells(1, xCount)
        xWb.Close False
        xFile = Dir
        xCount = xSht.Cells(3, Columns.Count).End(xlToLeft).Column + 1
    Loop
    Application.ScreenUpdating = True
    Exit Sub
ErrHandler:
    MsgBox "no files csv", , "Kutools for Excel"
End Sub 

doc import multiple csv text xml 8


If you want to import all XML files from a folder into a single sheet, you can use below VBA code.

1. Select a blank sheet you want to place the imported data, and press Alt + F11 keys to enable Microsoft Visual Basic for Applications window.

2. Click Insert > Module, paste VBA code into the Module window.

VBA: Import XML files from a folder into a worksheet.

Sub From_XML_To_XL()
'UpdatebyKutoolsforExcel20151214
    Dim xWb As Workbook
    Dim xSWb As Workbook
    Dim xStrPath As String
    Dim xFileDialog As FileDialog
    Dim xFile As String
    Dim xCount As Long
    On Error GoTo ErrHandler
    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    xFileDialog.AllowMultiSelect = False
    xFileDialog.Title = "Select a folder [Kutools for Excel]"
    If xFileDialog.Show = -1 Then
        xStrPath = xFileDialog.SelectedItems(1)
    End If
    If xStrPath = "" Then Exit Sub
    Application.ScreenUpdating = False
    Set xSWb = ThisWorkbook
    xCount = 1
    xFile = Dir(xStrPath & "\*.xml")
    Do While xFile <> ""
        Set xWb = Workbooks.OpenXML(xStrPath & "\" & xFile)
        xWb.Sheets(1).UsedRange.Copy xSWb.Sheets(1).Cells(xCount, 1)
        xWb.Close False
        xCount = xSWb.Sheets(1).UsedRange.Rows.Count + 2
        xFile = Dir()
    Loop
    Application.ScreenUpdating = True
    xSWb.Save
    Exit Sub
ErrHandler:
    MsgBox "no files xml", , "Kutools for Excel"
End Sub

3. Click Run button or F5 key to run the VBA, and select a folder in the popping dialog, see screenshot:

doc import multiple csv text xml 9

4. Click OK, and all the XML files in the selected folder are imported into the active sheet.


If you are not familiar with VBA, do worry, here I introduce a handy tool – Kutools for Excel for you. With its powerful Combine utility, you can quickly combine multiple xml files or csv files into one workbook or one Excel sheet.

Kutools for Excel, with more than 120 handy Excel functions, enhance your working efficiency and save your working time.

After free installing Kutools for Excel, please do as below:

1. Active Excel, and click Enterprise > Combine. See screenshot:
doc combine 1

2. And in the step 1 of Combine dialog, choose one separation option as you need. See screenshot:
doc combine 2

3. Click Next to go to step 2 of the Combine, click Add to add files from various folders or files from one folder to Workbook list, and also you can specify the sheets you want to combine from Worksheet list of right section. See screenshot:
doc combine 3

4. Click Next to the last one step of Combine, and you can specify the options.

1) Specify the title row number

2) Decide if insert worksheet information

3) Skip blank range or not

4) Specify paste option
doc combine 4

5. Click Finish, a dialog pops out to remind you select a location to save the new combined result. See screenshot:
doc combine 5

6. Click Save. All adding sheets have been combine into a new single sheet.
doc combine 6

Tip: With Combine, you also can combine multiple CSV files form multiple folders or one folder into one sheet or workbook.


If you want to export each sheet as csv/text/pdf file to a folder, Kutools for Excel’s Split Workbook utility can do a favor for you.

After free installing Kutools for Excel, please do as below:

1. Enable the workbook you want to export its worksheets, and click Enterprise > Workbook > Split Workbook. See screenshot:

doc import multiple csv text xml 10

2. In the Split Workbook dialog, you can check the sheet names you need to export, in default all the sheets are checked, and check Specify save format and select the file format you want to save as from below drop down list. See screenshot:

doc import multiple csv text xml 11

3. Click Split and select a folder that to save the split files in the Browse for Folder dialog, see screenshot:

doc import multiple csv text xml 12

4. Click OK, now all the checked sheets are exported as new file format in the selected folder.


quickly convert multiple XLSX. files to XLS or PDF. files in once time

In many cases, you may want to convert between XLSX and XLS, or convert a workbook to PDF file, but in Excel, you only can convert one workbook once time, have you ever imaged to convert multiple workbooks at once time? With Kutools for Excel's File Format Converter, you can quickly convert multiple workbook to pdf/97-2003 workbook/2007 or higher workbook at once and save them in a folder, at the same time, a converted report is generated!  Click for 60 days free trial!
doc file converter
 
Kutools for Excel: with more than 200 handy Excel add-ins, free to try with no limitation in 60 days.

Relative Articles:


Recommended Productivity Tools

Office Tab

gold star1 Bring handy tabs to Excel and other Office software, just like Chrome, Firefox and new Internet Explorer.

Kutools for Excel

gold star1 Amazing! Increase your productivity in 5 minutes. Don't need any special skills, save two hours every day!

gold star1 200 New Features for Excel, Make Excel Much Easy and Powerful:

  • Merge Cell/Rows/Columns without Losing Data.
  • Combine and Consolidate Multiple Sheets and Workbooks.
  • Compare Ranges, Copy Multiple Ranges, Convert Text to Date, Unit and Currency Conversion.
  • Count by Colors, Paging Subtotals, Advanced Sort and Super Filter,
  • More Select/Insert/Delete/Text/Format/Link/Comment/Workbooks/Worksheets Tools...

Screen shot of Kutools for Excel

btn read more      btn download     btn purchase

Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
People in conversation:
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Amanda · 6 days ago
    Hello, I tried running the code for .xml and it didn't appear to work as I got the message "no files xml". How do fix this?
  • To post as a guest, your comment is unpublished.
    see · 2 months ago
    Totally amazing! Tried the open all csv files in one worksheet and it worked beautifully. Thanks so much, really!
  • To post as a guest, your comment is unpublished.
    Chad · 7 months ago
    Mike: "the code for converting from txt files did not work for me."

    I had the same problem.The macro errors out if you have more txt files to open than worksheets in your workbook (default is 3).
    I fixed it by adding the following just before the loop exits so the macro creates a new sheet after the rest
    ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
  • To post as a guest, your comment is unpublished.
    zoi · 8 months ago
    Hi! The code "Import csv files from a folder into one worksheet", works for me! But this code doesn't work for an empty .txt file. Is there any way to keep also the empty files?
    • To post as a guest, your comment is unpublished.
      Sunny · 8 months ago
      Sorry, I do not know why need to import an empty txt.file~Just insert blank rows to seperate the contents.
  • To post as a guest, your comment is unpublished.
    Mike · 9 months ago
    the code for converting from txt files did not work for me.
    • To post as a guest, your comment is unpublished.
      Sunny · 9 months ago
      Sorry this article do not work for you. Could you describe you problem? What happen while runing the code? And what system you work?