vba - 输入超过文件VBA excel的结尾

标签 vba excel

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

    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Ќг‰®ґн нќРР^±>_‰
当宏尝试读取此行时,error 62发生 Input past end of file .

我该如何解决这个问题?

最佳答案

我可以让您对在 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

关于vba - 输入超过文件VBA excel的结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20128115/

相关文章:

excel - 如何检测工作表上的过滤器是否更改?

python - 有没有办法在 XlsxWriter 中创建图表而不将数据写入工作表中的单元格? IE。来自 python 脚本中的数组?

Excel 函数不会在导出的文件中计算

vba - 隐藏或取消隐藏所有 Excel 工作表而不循环

excel - VBA:无法在另一张表中引用范围

vba - 使用密码命名工作簿

VBA Excel : Assigning range values to a new range

vba - Excel VBA 循环遍历数据透视项

arrays - VBA:函数数组,ReDim 给出无效的 ReDim

vba - Excel VBA : How to push data to an exclusive range on another sheet?