excel - 如何将多个文本文件打开到一个 Excel 工作表中,但每个文本文件跳过第 1 行?

标签 excel file vba text import

使用下面的代码,我可以将文件夹中的所有文本文件打开到一张 Excel 表中。每个文件都有标题,我不知道如何让它跳过每个文本文件的第一行。

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("\\mydirectoryfolderhere")
' 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(1, 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

最佳答案

像这样的东西(未经测试)

Dim firstline as boolean
'.....
For Each file In folder.Files
Set FileText = file.OpenAsTextStream(ForReading)
firstline=True

Do While Not FileText.AtEndOfStream
    TextLine = FileText.ReadLine

    If Not firstline then

        Items = Split(TextLine, "|")
        For i = 0 To UBound(Items)
            cl.Offset(1, i).Value = Items(i)
        Next
        Set cl = cl.Offset(1, 0)

    End If

    firstline=False
Loop

' Clean up
FileText.Close
Next file
'....

关于excel - 如何将多个文本文件打开到一个 Excel 工作表中,但每个文本文件跳过第 1 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12918992/

相关文章:

java - 如何使用 Apache POI 从 Excel 文件读取值

c++ - 从文件中获取输入并将其保存到临时内存中?

java - 系统找不到指定的路径

excel - 基于特定单元格值缩进单元格的 VBA 代码

vba - 选择所有 "visible"工作表(打印为 pdf)

vba - 删除前两列中的值,但第一列中的值等于工作表名称的行除外 VBA

excel - 如何在循环访问一系列单元格时获取当前单元格编号(Excel VBA)

javascript - HTML 表格到 Excel (xls) 使用 javascript/jQuery

Excel Sumif 公式

iPhone,在沙箱中写入csv文件