excel copy 2nd column from multiple txt files

bjcowen90000

New Member
Joined
Nov 10, 2014
Messages
17
I have a folder with a lot of files of the form: defects_*.txt

Assuming the path to the directory containing these files is C:/Path, how can I load just the second column of these txt files into excel?

So column A would be the 2nd column of defects_002.txt, column B would be the 2nd column of defects_005.txt, etc (no constant increment). Can VBA do this, or do I need to use something else?
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
For instance, one txt file is below. Noe that the letters are all on 1 line, and I don't care about the headers or the first column. In this case I would extract all the 0's, then open the next txt file and paste the 2nd column etc.

# "Timestep" "Ca_Vac"
109800 0
109801 0
109700 0
109701 0
109700 0
109701 0

<tbody>
</tbody>
 
Last edited:
Upvote 0
Try this, change "Sheet1" if needed

Code:
 Sub textRead()
    Dim defects As String
    Const uPath As String = "C:\Path\"
    tFile = Dir(uPath & "defects_*.Txt")
    c = 1
    Do While tFile <> ""
        Open uPath & tFile For Binary As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
            defects = Space$(LOF(1))
            Get [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , , defects
        Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
        dLines = Split(defects, vbCrLf)
        r = 1
        For i = 1 To UBound(dLines)
            Sheets("Sheet1").Cells(r, c) = Split(dLines(i))(1)
            r = r + 1
        Next
        tFile = Dir
        c = c + 1
    Loop
 End Sub
 
Upvote 0

Forum statistics

Threads
1,216,495
Messages
6,130,979
Members
449,611
Latest member
Bushra

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