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
Import multiple text files from one folder into a single sheet with VBA
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
Sub Test()
'UpdatebyExtendoffice6/7/2016
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)
End If
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"
Exit Sub
End If
Do While xFile <> ""
xFiles.Add xFile, xFile
xFile = Dir()
Loop
Set xToBook = ThisWorkbook
If xFiles.Count > 0 Then
For I = 1 To xFiles.Count
Set xWb = Workbooks.Open(xStrPath & xFiles.Item(I))
xWb.Worksheets(1).Copy after:=xToBook.Sheets(xToBook.Sheets.Count)
On Error Resume Next
ActiveSheet.Name = xWb.Name
On Error GoTo 0
xWb.Close False
Next
End If
End Sub
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.
Import text file to the active cell with Kutools for Excel
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 300 handy functions, makes your jobs more easier. | ||
After free installing Kutools for Excel, please do as below:
1. Select a cell you want to import the text file, and click Kutools Plus > 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:
Import/Insert text file at the cursor
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!















