导入.csv文件以使用行之间的空格进行excel

时间:2018-06-17 19:52:04

标签: excel-vba import-from-excel vba excel

我正在尝试将.CSV文件(从电子邮件outlook导入)导入Excel工作表,.CSV文件包含6行,第5行为空,在此代码下excel导入前4行并忽略第6行,错误是“下标是放置范围”我该如何解决这个问题。提前致谢     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
        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

End Sub

1 个答案:

答案 0 :(得分:0)

如果您执行空行的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