Give this a try:
Option Explicit
Sub ImportTextFiles()
Dim txtPath As String
Dim txtFile As String
Dim txtName As String
Dim wsBase As Worksheet
Dim FR As Long, NR As Long
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'Imported to this worksheet
Set wsBase = ThisWorkbook.Sheets("Sheet1")
'remember the final \ in this string
txtPath = "C:\2011\Text\"
'clear existing report
If wsBase.UsedRange.Rows.Count > 1 Then wsBase.UsedRange.Offset(1).ClearContents
NR = 2
'Start looping through text files in noted folder
txtFile = Dir(txtPath & "*.txt")
Do While Len(txtFile) > 0
FR = NR
txtName = Replace(txtFile, ".txt", "")
ActiveWorkbook.Worksheets.Add
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & txtPath & txtFile, Destination:=ActiveSheet.Range("A1"))
.Name = txtName
.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 = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
ActiveSheet.Range("A3").CurrentRegion.Offset(1).Copy wsBase.Range("C" & FR)
NR = wsBase.Range("C" & wsBase.Rows.Count).End(xlUp).Row + 1
wsBase.Range("B" & FR & ":B" & NR - 1) = txtName
ActiveSheet.Delete 'delete temp sheet
txtFile = Dir 'get next text filename
Loop
wsBase.Columns.AutoFit 'cleanup
Application.ScreenUpdating = True
End Sub
Bookmarks