VBA EOF Function

ghost dog1

Seniorius Lurkius
5
Hi,

I have a text file that was produced by a Unix program in CSV format. I am trying to parse it using Excel and thought I could automate the task using Excel VBA.

However, the script I wrote VBA is not detecting the end of file and the loop goes infinite. It does not exit the Do Loop when all the data has been read in. Any help would be greatly appreciated.

Sub OpenTextFile()
Dim FilePath As String
Dim columns As Long

FilePath = "C:\Temp\Data.csv"

Open FilePath For Input As #1

row_number = 0
columns = 0

Do Until EOF(1)

Line Input #1, LineFromFile

LineItems = Split(LineFromFile, ",")

If row_number > 0 Then

Do While columns < 16
ActiveCell.Offset(row_number, columns).Value = LineItems(columns)
columns = columns + 1
Loop
columns = 0

End If

row_number = row_number + 1

Loop

Close #1

End Sub
 

Jonathon

Ars Legatus Legionis
17,646
Subscriptor
The other day I was trying to do something similar in Python and found out it doesn't have an EOF function. WTF?

They say "you're end the end of the file when you try to read and get a zero-length result". That's certainly not true when doing non-blocking reads from the network or keyboard.

But for files it works.
Python sockets aren't files, and, if you want to treat one as a file-like object (with socket.makefile()), the socket must be in blocking mode (so calls to read always either return data, or the socket's at EOF).

Yes, that means Python's generic I/O streams are less generic than they are in some other languages. It's also unlikely to change any time soon, because requiring EOF checking would make simple synchronous I/O more complex and less Pythonic. If you're building something where this actually matters, the asyncio streams do have EOF functions.