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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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.

[TABLE="width: 64"]
<tbody>[TR]
[TD="width: 64"]# "Timestep" "Ca_Vac"[/TD]
[/TR]
[TR]
[TD]109800 0[/TD]
[/TR]
[TR]
[TD]109801 0[/TD]
[/TR]
[TR]
[TD]109700 0[/TD]
[/TR]
[TR]
[TD]109701 0[/TD]
[/TR]
[TR]
[TD]109700 0[/TD]
[/TR]
[TR]
[TD]109701 0[/TD]
[/TR]
</tbody>[/TABLE]
 
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,226,694
Messages
6,192,473
Members
453,726
Latest member
JoeH57

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