Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input past end of file VBA excel

Tags:

excel

vba

I am attempting to read text file (these files are produced by external program, that could not be tweeked) using the following macro.

    While Not EOF(int_current_file)
    Line Input #int_current_file, buf
    If Left(buf, 1) <> "#" Then
        buf1 = Split(buf, "=")
        Print #int_master_file, CInt(Left(buf, 2)) & ";" & CInt(Mid(buf, 3, 4)) & ";" & CInt(Mid(buf, 7, 3)) & ";" & CInt(Mid(buf, 10, 1)) & ";" & CDbl(buf1(1) / 100000000) & ";" & CDate(file_date) & ";" & Mid(file_name, 4, 3)
    End If
    'Line Input #int_current_file, buf
    'Debug.Print Data
Wend

However, at the second line of this file I have the following string:

=01082013=01072013=31072013=06082013=1640=380441=21=000001249=#02IFS86.G84=IFSSS5=7ҐK!Ђi—Љ42ЃЁ4№{¤Хo$]ґ•Хp Ё1‹;±~†ЁRLЌг‰®ґн нќРР^±>_‰

When macro tries to read this line the error 62 occurs Input past end of file.

How can I fix this problem?

like image 301
mr.M Avatar asked Nov 21 '13 17:11

mr.M


People also ask

How do I fix input past end of file?

To correct this error Use the EOF function immediately before the Input statement to detect the end of the file. If the file is opened for binary access, use Seek and Loc .

What is EOF in Access VBA?

Remarks. Use EOF to avoid the error generated by attempting to get input past the end of a file. The EOF function returns False until the end of the file has been reached. With files opened for Random or Binary access, EOF returns False until the last executed Get statement is unable to read an entire record.

When there is no more data to be read from file what EOF () function will return in Boolean?

C++ provides a special function, eof( ), that returns nonzero (meaning TRUE) when there are no more data to be read from an input file stream, and zero (meaning FALSE) otherwise. Rules for using end-of-file (eof( )): 1.


1 Answers

May I interest you in a better way of reading text files in VBA?

This will read the entire text file in ONE GO in an array and then close the file. This way you don't need to keep the file open at all times.

Option Explicit

Sub Sample()
    Dim MyData As String, strData() As String
    Dim i As Long

    '~~> Replace your file here
    Open "C:\MyFile.Txt" For Binary As #1
    MyData = Space$(LOF(1))
    Get #1, , MyData
    Close #1
    strData() = Split(MyData, vbCrLf)

    '
    '~~> Now strData has all the data from the text file
    '

    For i = LBound(strData) To UBound(strData)
        Debug.Print strData(i)
        '
        '~~> What ever you want here
        '
    Next i
End Sub
like image 84
Siddharth Rout Avatar answered Nov 03 '22 00:11

Siddharth Rout