このサイトでこのコードを見つけましたが、自分のニーズに合わせて調整することはできませんでしたが、非常に迅速な修正に違いないと思います.
このコードは、一連のテキスト ファイルを Excel にインポートします。ファイルが開かれ、このファイルの 1 行目が A1 に配置され、2 行目が A2 に配置されます。新しいファイルを開くと、テキストは列 A の次に使用可能なセルに配置されます (すべてのファイルが列 A に読み込まれます)。
少し修正を加えたいと思います。ファイル 1 の最初の行を A1 に、2 行目を B1 に、というようにします (つまり、ファイル 1 のすべての行が行 1 に保持されます)。次に、ファイル 2 の行は行 2 に配置され、ファイル 3 は行 3 に配置されます。
どんな助けでも大歓迎です!
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