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

or

How to import multiple text files to multiple sheets?

Supposing, there are multiple text files in a folder of your computer, now, you want to import these text files to an Excel workbook, and place each text file into separate worksheets. Copying and pasting each text file to the worksheet one by one will waste lots of time, here, I can talk about some tricks for you to solve this problem.

Import multiple text files to separate worksheets with VBA code

Split a workbook to multiple separate text / pdf / csv / xlsx files with Kutools for Excel


Combine / import multiple worksheets into one worksheet or workbook:
In your daily work, to combine multiple worksheets or workbooks into one single worksheet or workbook may be a huge and headachy work. But, if you have Kutools for Excel, with its powerful utility – Combine, you can quickly combine multiple worksheets, workbooks into one worksheet or workbook.
doc combine multiple worksheets

arrow blue right bubble Import multiple text files to separate worksheets with VBA code


The following VBA code may help you to import each text file into separate sheets within a new workbook at once, please do as follows:

1. Hold down the ALT + F11 keys in Excel to open the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: Import multiple text files to separate worksheets:

Sub CombineTextFiles()
'updateby Extendoffice 20151015
    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. After pasting the above code, press F5 key to run this code, and in the popped out window, please specify the folder which contains the text files, go to the folder to select the text files you want to import to the worksheets, see screenshot:

doc import multiple text files 1

4. Then click Open button, all the selected text files have been imported into a new workbook and each file located in one worksheet individually.

5. At last, you can save the new workbook as you need.


arrow blue right bubble Split a workbook to multiple separate text / pdf / csv / xlsx files with Kutools for Excel

Sometimes, you may need to do some opposite operations of above task, that is to say, you need to split a workbook to separate text files. Kutools for Excel’s Split Workbook utility can help you to solve this job quickly and easily.

Kutools for Excel : with more than 300 handy Excel add-ins, free to try with no limitation in 60 days

After installing Kutools for Excel, please do as follows:( Free Download Kutools for Excel Now! )

1. Activate the workbook that you want to split to multiple text files.

2. Click Enterprise > Workbook > Split Workbook, see screenshot:

excel add-ins split workbook feature

3. In the Split Workbook dialog box:

(1.) Check the worksheets that you want to split.

(2.) Check Specify save format, then choose the file format that you want to save, for example, you can choose txt, csv, pdf, xlsx or xls file format as you need.

(3.) Then click Split button, in the following prompt box specify a folder to output the separate files.

excel add-ins split workbook dialog box  2 excel add-ins select location

4. Then click OK button, and your selected worksheets will be split and save to your desired folder with the format as you need.

excel add-ins separate files

Free Download Kutools for Excel Now!


Recommended Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 71%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 82% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Thuva · 1 years ago
    thank you , multiple text files to different worksheets code worked!
  • To post as a guest, your comment is unpublished.
    pinky · 2 years ago
    Thanks for this code this is working great.. i want to merge all the selected sheet into a single sheet is that possible..
  • To post as a guest, your comment is unpublished.
    Raul · 2 years ago
    How do I change this macros to display text file saved as UTF-8 or in other words using Platform = 65001, Thanks
  • To post as a guest, your comment is unpublished.
    Vaibhav · 2 years ago
    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.
  • To post as a guest, your comment is unpublished.
    G · 2 years ago
    Is it possible to open Sequencially multiple txt files in a single sheet ?