输入文件VBA excel的结尾

mr.*_*r.M 4 excel vba excel-vba

我正在尝试使用以下宏读取文本文件(这些文件由外部程序生成,无法编写).

    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
Run Code Online (Sandbox Code Playgroud)

但是,在此文件的第二行,我有以下字符串:

=01082013=01072013=31072013=06082013=1640=380441=21=000001249=#02IFS86.G84=IFSSS5=7?K!?i—?42??4?{¤?o$]?•?p ?1‹;±~†?RL??‰®?? ????^±>_‰

当宏尝试读取此行时,error 62会发生Input past end of file.

我该如何解决这个问题?

Sid*_*out 13

我是否有兴趣以更好的方式阅读VBA中的文本文件?

这将读取ONE GO数组中的整个文本文件,然后关闭该文件.这样,您无需始终保持文件处于打开状态.

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
Run Code Online (Sandbox Code Playgroud)