将 .csv 文件导入到 excel,行与行之间有 space

import .csv file to excel with space between lines

我正在尝试将 .CSV 文件(从电子邮件 outlook 导入)导入到 excel sheet,.CSV 文件包含 6 行,第 5 行为空,在此代码下 excel 只导入前 4 行并忽略第 6 行,错误是 "Subscript is put of range" 我该如何解决这个问题。提前致谢 子 OpenOneFile()

Dim FilePath As String

FilePath = "D:\Excel\Learning Excel VBA\Outlook VBA\Email1.csv"
Open FilePath For Input As #1

    Do Until EOF(1)
        Line Input #1, linefromfile
        Lineitems = Split(linefromfile, ":")

        ActiveCell.Offset(row_number, 0).Value = Lineitems(0)
        ActiveCell.Offset(row_number, 1).Value = Lineitems(1)

        row_number = row_number + 1

    Loop
Close #1

结束子

当你对一个空行做Split时,你不能引用Lineitems (0),因为没有值。 试试这个:

Sub OpenOneFile()
Dim FilePath As String

FilePath = "D:\Excel\Learning Excel VBA\Outlook VBA\Email1.csv"

Open FilePath For Input As #1

Do Until EOF(1)
    Line Input #1, linefromfile
    If linefromfile <> "" Then
        Lineitems = Split(linefromfile, ":")

        ActiveCell.Offset(row_number, 0).Value = Lineitems(0)
        ActiveCell.Offset(row_number, 1).Value = Lineitems(1)

        row_number = row_number + 1
    End If
Loop
Close #1
End Sub