Excel 为什么在运行Windows更新后,我会收到“运行时错误62输入超过文件末尾”?
你知道为什么在使用输入函数时,我一直在使用下面的代码得到一个运行时错误62 Input over end of file error吗。帮助函数告诉我文件是二进制的,我应该使用LOF或Seek,但是两者都不起作用。在我的计算机最近更新Windows和Microsoft之前,此代码一直运行良好Excel 为什么在运行Windows更新后,我会收到“运行时错误62输入超过文件末尾”?,excel,vba,Excel,Vba,你知道为什么在使用输入函数时,我一直在使用下面的代码得到一个运行时错误62 Input over end of file error吗。帮助函数告诉我文件是二进制的,我应该使用LOF或Seek,但是两者都不起作用。在我的计算机最近更新Windows和Microsoft之前,此代码一直运行良好 Dim fldr As FileDialog Dim sItem As String Set fldr = Application.FileDialog(msoFileDialog
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFilePicker)
With fldr
.Filters.Clear
.Filters.Add "All files", "*.*"
.Title = "Select a CFG File to Convert fromatting from R2013 to 1991."
.AllowMultiSelect = False
.InitialFileName = ActiveWorkbook.Path 'Application.DefaultFilePath
If .Show <> -1 Then Exit Sub
sItem = .SelectedItems(1)
End With
set fldr = Nothing
Open sItem For Input As #1
dataArray = Split(Input(LOF(1), #1), vbLf)
Close #1
If Len(dataArray(2)) - Len(Replace(dataArray(2), ",", "")) = 9 Then
MsgBox "It appears the comtrade file format already conforms to the 1991 standard version." & vbNewLine & "" & vbNewLine & "Conversion was Aborted."
Exit Sub
End If
我正在尝试计算所选文件第3行中的逗号数
对于一行代码来说,这需要大量的工作
您没有验证整个文件,只有第二行。您还硬编码文件句柄,这可能会导致其他问题-使用FreeFile函数从VBA获取自由文件句柄,而不是假设1可用。或者更好地使用更高抽象的FileSystemObject,而不是引用Microsoft脚本运行时库:
或者,在不引用脚本库的情况下:
注意,当您针对对象编写代码时,成员调用是后期绑定的:您不会得到IntelliSense/autocompletion,也不会得到任何编译时验证;打字错误将在运行时愉快地编译并放大,错误为438。无论何处,我都喜欢早期绑定的代码——但我想不出对脚本库使用后期绑定的理由,因为该库在本世纪构建的每台Windows机器上都是完全相同的。这不是问题,但fromatting可能应该是格式化-FWIW非常类似的文件读取器代码以前也被破坏过,请参阅-我怀疑Windows/Office Update与此有关。
dataArray = Split(Input(LOF(1), #1), vbLf)
With New Scripting.FileSystemObject
With .OpenTextFile(filename, ForReading)
Dim contents As String
contents = .ReadAll
End With
End With
Dim lines As Variant
lines = Split(contents, vbNewLine)
Const ForReading As Long = 1
With CreateObject("Scripting.FileSystemObject")
With .OpenTextFile(filename, ForReading)
Dim contents As String
contents = .ReadAll
End With
End With
Dim lines As Variant
lines = Split(contents, vbNewLine)