我正在尝试使用以下宏读取文本文件(这些文件是由外部程序生成的,无法调整)。
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/