0

このサイトでこのコードを見つけましたが、自分のニーズに合わせて調整することはできませんでしたが、非常に迅速な修正に違いないと思います.

このコードは、一連のテキスト ファイルを 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
4

2 に答える 2

0

うん。とても簡単。列と行のオフセット方法を調整し、読み取り時に各行を区切らないようにする必要がありました。

以下の調整されたコードを参照してください。

Sub ReadFilesIntoActiveSheet()

Dim fso As FileSystemObject
Dim folder As folder, file As file, FileText As TextStream
Dim TextLine As String, Items() As String
Dim i As Long, cl As Range

' Get a FileSystem object
Set fso = New FileSystemObject

' get the directory you want
Set folder = fso.GetFolder("D:\YourDirectory\")

Dim x As Long
x = 1 'to offset rows for each file

' Loop thru all files in the folder
For Each file In folder.Files

    ' set the starting point to write the data to
    Set cl = ActiveSheet.Cells(x, 1)

    ' Open the file
    Set FileText = file.OpenAsTextStream(ForReading)

    Dim i As Long
    i = 0 'to offset columsn for each line
    ' Read the file one line at a time
    Do While Not FileText.AtEndOfStream

        TextLine = FileText.ReadLine 'read line

        cl.Offset(, i).Value = TextLine 'fill cell

        i = i + 1
    Loop

    ' Clean up
    FileText.Close

    x = x + 1

Next file

Set FileText = Nothing
Set file = Nothing
Set folder = Nothing
Set fso = Nothing

End Sub
于 2012-10-24T15:16:05.677 に答える
0

行と列へのすべての参照を互いに置き換えるだけで十分だと思います。試す:

  1. cl.Offset(0, i).Value = Items(i)と置き換えますcl.Offset(i, 0).Value = Items(i)

  2. Set cl = cl.Offset(1, 0)と置き換えますSet cl = cl.Offset(0, 1)

それはトリックを行いますか?

于 2012-10-24T15:13:07.343 に答える