Hi all,
I have been using a bit of VBA code that I found online to import many text files into one excel spreadsheet.
I have thousands of text files that contain a multi-line header, and then two columns of numerical data, tab delimited. I do not need to import the header, but I require the data to all be placed into seperate columns into a single spreadsheet.
Currently, with the code I am using, ONLY THE SECOND COLUMN of data gets imported into the spreadsheet. This is great for some of my needs, but sometimes I require BOTH columns to be put into excel.
I understand only the basics of VB and I am having trouble finding out which line of the code directs only the addition of the second column of data into my sheet. Can someone show me how to identify and modify it to bring in both? I would like the two columns of data (.txt) to come into seperate columns in the spreadsheet, ie, as A and B. Then, as the code loops through the next text file, it should bring in the next two columns of data into columns C and D, etc etc.
I would also like to know which line in this code brings in the name of my text file and places it as the first entry in the column. I like this, but cannot "see" it either.
Here it is:
Sub test()
Dim myDir As String, fn As String, ff As Integer, txt As String
Dim delim As String, n As Long, b(), flg As Boolean, x, t As Integer
myDir = "C:\Users\...."
delim = vbTab
fn = Dir(myDir & "\*.txt")
Do While fn <> ""
ReDim b(1 To Rows.Count, 1 To 1)
ff = FreeFile
Open myDir & "\" & fn For Input As #ff
Do While Not EOF(ff)
Line Input #ff, txt
x = Split(txt, delim)
If Not flg Then
n = n + 1: b(n, 1) = fn
End If
If UBound(x) > 0 Then
n = n + 1
b(n, 1) = x(1)
End If
flg = True
Loop
Close #ff
flg = False
t = t + 1
ThisWorkbook.Sheets(1).Cells(1, t).Resize.Value = b
n = 0
fn = Dir()
Loop
End Sub
Thanks so much for your help.
I have been using a bit of VBA code that I found online to import many text files into one excel spreadsheet.
I have thousands of text files that contain a multi-line header, and then two columns of numerical data, tab delimited. I do not need to import the header, but I require the data to all be placed into seperate columns into a single spreadsheet.
Currently, with the code I am using, ONLY THE SECOND COLUMN of data gets imported into the spreadsheet. This is great for some of my needs, but sometimes I require BOTH columns to be put into excel.
I understand only the basics of VB and I am having trouble finding out which line of the code directs only the addition of the second column of data into my sheet. Can someone show me how to identify and modify it to bring in both? I would like the two columns of data (.txt) to come into seperate columns in the spreadsheet, ie, as A and B. Then, as the code loops through the next text file, it should bring in the next two columns of data into columns C and D, etc etc.
I would also like to know which line in this code brings in the name of my text file and places it as the first entry in the column. I like this, but cannot "see" it either.
Here it is:
Sub test()
Dim myDir As String, fn As String, ff As Integer, txt As String
Dim delim As String, n As Long, b(), flg As Boolean, x, t As Integer
myDir = "C:\Users\...."
delim = vbTab
fn = Dir(myDir & "\*.txt")
Do While fn <> ""
ReDim b(1 To Rows.Count, 1 To 1)
ff = FreeFile
Open myDir & "\" & fn For Input As #ff
Do While Not EOF(ff)
Line Input #ff, txt
x = Split(txt, delim)
If Not flg Then
n = n + 1: b(n, 1) = fn
End If
If UBound(x) > 0 Then
n = n + 1
b(n, 1) = x(1)
End If
flg = True
Loop
Close #ff
flg = False
t = t + 1
ThisWorkbook.Sheets(1).Cells(1, t).Resize.Value = b
n = 0
fn = Dir()
Loop
End Sub
Thanks so much for your help.