Importing Multiple .txt files

MrLookout9

New Member
Joined
Jun 25, 2011
Messages
27
Hello and thank you for your expertise.

Here is my situation. I have approx. 500 text files all residing in one folder. This folder only contains text files. Each text file has approx. 100 rows of data, tab delimited.

I would like to import every row from each file into one sheet in excel.

I think this can be done with a macro, but would be open to a different approach.

Thanks,

Adam
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Code:
Sub test1()
 
Dim objfile As Variant
Dim objfolder As Variant
Dim objfs As Variant
Dim lastactiverow As Long
 
Set objfs = CreateObject("scripting.filesystemobject")
Set objfolder = objfs.getfolder(urfolderdestination)
lastactiverow = 1
 
For Each objfile In objfolder.Files
    If Right(objfile.Name, 4) = ".txt" Then
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;urfolderdestination" & objfile.Name, Destination:= _
            Range("A" & lastactiverow))
            .Name = "test"
            .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 = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        lastactiverow = ActiveSheet.UsedRange.Rows.Count + 1
    Else
    End If
Next objfile
End Sub

Change the instances of "urfolderdestination" to your actual folder path
 
Upvote 0

Forum statistics

Threads
1,226,729
Messages
6,192,696
Members
453,747
Latest member
tylerhyatt04

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top