I found this code on this site but I haven’t been able to adjust it for my own needs though I think it must be a very quick fix.
The code imports a series of text files to excel. A file is opened and the first line of this file is placed in A1, the second line in A2 and so on. When a new file is opened, the text is placed in the next available cell in column A (all files are read into column A).
I want to make a slight modification. I want the first line of file 1 in A1, the second line in B1 and so on (i.e. all the lines from File 1 are kept in Row 1). Then, the lines in File 2 are placed in Row 2, File 3 in Row 3 etc.
Any help would be greatly appreciated!
Sub ReadFilesIntoActiveSheet()
Dim fso As FileSystemObject
Dim folder As folder
Dim file As file
Dim FileText As TextStream
Dim TextLine As String
Dim Items() As String
Dim i As Long
Dim cl As Range
' Get a FileSystem object
Set fso = New FileSystemObject
' get the directory you want
Set folder = fso.GetFolder("D:\YourDirectory\")
' set the starting point to write the data to
Set cl = ActiveSheet.Cells(1, 1)
' Loop thru all files in the folder
For Each file In folder.Files
' Open the file
Set FileText = file.OpenAsTextStream(ForReading)
' Read the file one line at a time
Do While Not FileText.AtEndOfStream
TextLine = FileText.ReadLine
' Parse the line into | delimited pieces
Items = Split(TextLine, "|")
' Put data on one row in active sheet
For i = 0 To UBound(Items)
cl.Offset(0, i).Value = Items(i)
Next
' Move to next row
Set cl = cl.Offset(1, 0)
Loop
' Clean up
FileText.Close
Next file
Set FileText = Nothing
Set file = Nothing
Set folder = Nothing
Set fso = Nothing
End Sub
Yeap. Pretty easy. Just needed to adjust how your columns and rows are being offset and to not have it delimit each line as it’s read.
See the adjusted code below: