i am trying to import a .CSV file(imported from email outlook) to excel sheet, .CSV file contains 6 lines, line number 5 is empty, under this code excel import just the first 4 lines and ignore Line 6, error is "Subscript is put of range" how can i solve this. thanks in advance 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
When you do Split
of an empty line, you can not reference Lineitems (0)
because there are no values.
Try this:
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