繁体   English   中英

将文本文件导入Excel-每个文件中的行都放在同一行的单独列中

[英]Importing text files to Excel - lines from each file are placed in separate columns on same row

我在此站点上找到了此代码,但是我无法根据自己的需要对其进行调整,尽管我认为它必须是非常快速的解决方案。

该代码将一系列文本文件导入excel。 打开一个文件,该文件的第一行放在A1中,第二行放在A2中,依此类推。 当打开一个新文件时,文本将放置在A列中的下一个可用单元格中(所有文件都被读入A列)。

我想稍作修改。 我想要文件A1中的第一行,文件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

我认为将所有对行和列的引用相互替换就足够了。 尝试:

  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)

这有招吗?

是的 相当容易。 只需调整列和行的偏移方式,并在读取时不限制每行的边界。

请参阅以下调整后的代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM