How to import multiple text files from a folder into one worksheet?
For instances, here you have a folder with multiple text files, what you want to do is to import these text files into a single worksheet as below screenshot show. Instead of copying the text files one by one, are there any tricks to quickly import the text files from one folder into one sheet?
Import multiple text files from one folder into a single sheet with VBA
Import text file to the active cell with Kutools for Excel
Export Range to File
|Kutools for Excel's Export Range to File function can export or save a range to separate file as
workbook, pdf, text, csv or text.
|Click Enterprise > Import/Export > Export Range to File.
Here is a VBA code can help you import all text files from one specific folder into a new sheet.
1. Enable a workbook you want to import text files, and press Alt + F11 keys to enable Microsoft Visual Basic for Applications window.
2. Click Insert > Module, copy and paste below VBA code to the Module window.
VBA: Import multiple text files from one folder to one sheet
Dim xWb As Workbook
Dim xToBook As Workbook
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
Dim xFiles As New Collection
Dim I As Long
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)
If xStrPath = "" Then Exit Sub
If Right(xStrPath, 1) <> "\" Then xStrPath = xStrPath & "\"
xFile = Dir(xStrPath & "*.txt")
If xFile = "" Then
MsgBox "No files found", vbInformation, "Kutools for Excel"
Do While xFile <> ""
xFiles.Add xFile, xFile
xFile = Dir()
Set xToBook = ThisWorkbook
If xFiles.Count > 0 Then
For I = 1 To xFiles.Count
Set xWb = Workbooks.Open(xStrPath & xFiles.Item(I))
On Error Resume Next
ActiveSheet.Name = xWb.Name
On Error GoTo 0
3. Press F5 to display a dialog, and select a folder which contains text files you want to import. See screenshot:
4. Click OK. Then the text files have been imported to the active workbook as new sheet separately.
If you want to import one text file to a specific cell or range, you can apply Kutools for Excel’s Insert File at Cursor utility.
| 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. Select a cell you want to import the text file, and click Enterprise > Import/ Export > Insert File at Cursor. See screenshot:
2. Then a dialog pops out, click Browse to display the Select a file to be inserted at the cell cursor position dialog, next select Text Files from the drop down list, and then choose the text file you want to import. See screenshot:
3. Click Open > Ok, and the specify text file has been inserted at the cursor position, see screenshot:
Recommended Productivity Tools
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!
- 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!