输入文件末尾的 VBA excel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20128115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Input past end of file VBA excel
提问by mr.M
I am attempting to read text file (these files are produced by external program, that could not be tweeked) using the following macro.
我正在尝试使用以下宏读取文本文件(这些文件是由外部程序生成的,不能被 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
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?г‰??н н?РР^±>_‰
=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
.
当宏尝试读取此行时,error 62
会发生Input past end of file
.
How can I fix this problem?
我该如何解决这个问题?
回答by Siddharth Rout
May I interest you in a better way of reading text files in VBA?
我可以让您对在 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.
这将读取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