How to import text files to Excel as separate sheets with original names in Excel?
Have you ever tried to import text files to Excel as separate sheets with the files’ original names in Excel? In this article, I introduce a VBA code to quickly import text files from one folder to Excel with the names.
Import text files to Excel with names
Import text files to Excel with names
1. Enable Excel, press Alt + F11 keys to enable Microsoft Visual Basic for Applications window, then click Insert > Module.
2. Copy and paste below code to the Module script.
VBA: Import text files with names
Sub LoadPipeDelimitedFiles()
'UpdatebyExtendoffice20181010
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
Dim xCount As Long
Dim xWS As Worksheet
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
Set xWS = Sheets(xCount)
xWS.Select
xWS.Name = "TEXT " & Left(xFile, Len(xFile) - 4)
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 to run the code, then select a folder which contains text files you want to import to Excel in the Select a folder dialog.
4. Click OK, the text files in the folder have been imported in Excel as single sheet with their own names.
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!
