【问题标题】:Input past end of file VBA excel输入超过文件VBA excel的结尾
【发布时间】:2013-11-22 02:08:25
【问题描述】:

我正在尝试使用以下宏读取文本文件(这些文件是由外部程序生成的,无法进行 tweeked)。

    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

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

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

当宏尝试读取这一行时,error 62 出现 Input past end of file

我该如何解决这个问题?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    能否让您了解一种在 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
    

    【讨论】:

    • 能否请您在此代码和平中发布 cmets:MyData = Space$(LOF(1)) Get #1, , MyData?
    • Space$(LOF(1)) 创建一个文件大小的字符串。 LOFSpace$ 是将字符串初始化为给定长度。顺便说一句,它对你有用吗?
    • 嗨,我对在 VBA 中导入简单文本文件的困难感到震惊!。直接从网站提取数据似乎比从文本文件中导入数据更容易。你的建议是我尝试过的第 20 个建议,它也对我不起作用。我只是想将重要的文本文件作为一个字符串,以便我可以在结果字符串上使用 'Mid' 和 'InStr' 函数。任何澄清都将非常受欢迎。如果我尝试将 strData 放在 TextBox 中,我会收到一个错误:“类型不匹配”我猜这是由于数组而不是字符串类型?有什么解决办法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 2013-04-16
    • 2013-10-20
    相关资源
    最近更新 更多