You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VBA运行时错误62:输入超出文件尾 - Excel VBA对接HEC-RAS求助

解决VBA处理HEC-RAS的.U04文件时的Run-time Error 62错误

问题描述

我用Excel VBA编写了对接HEC-RAS的.U04文本文件处理代码,正常运行3个月后,2月4日突然触发Run-time Error 62: Input Past End of File,错误出现在第44行Line Input #1, strLine: Print #2, strLine_new_3。我尝试将Do While Not EOF(1)改为Do Until EOF(1),但问题依旧,恳请协助解决。

原代码

Option Explicit

Sub newflowuns()

Dim HRC As New HECRASController
Dim strRASProject As String
strRASProject = "C:\Users\kom3\Documents\PADOLO\RAS_PADOLO_1D.prj"
HRC.Project_Open strRASProject

Dim strLine As String
Dim strPlanfile As String
Dim strNewPlanfile As String
Dim strFlowfile As String
Dim strNewFlowfile As String
Dim sngInflow As Single
Dim strLine_new_1 As String
Dim strLine_new_2 As String
Dim strLine_new_3 As String
Dim strLine_new_head As String
Dim strLine_old_head As String

strPlanfile = "C:\Users\kom3\Documents\PADOLO\RAS_PADOLO_1D.p14"
strNewPlanfile = "C:\Users\kom3\Documents\PADOLO\RAS_PADOLO_1D.tempp14"
strFlowfile = "C:\Users\kom3\Documents\PADOLO\RAS_PADOLO_1D.u04"
strNewFlowfile = "C:\Users\kom3\Documents\PADOLO\RAS_PADOLO_1D.tempu04"

Open strFlowfile For Input As #1
Open strNewFlowfile For Output As #2

strLine_old_head = "Flow Hydrograph= " & Sheet2.Range("CK2").Value
strLine_new_head = "Flow Hydrograph= " & Sheet2.Range("CJ2").Value

strLine_new_1 = Sheet2.Range("CJ4").Value
strLine_new_2 = Sheet2.Range("CK4").Value
strLine_new_3 = Sheet2.Range("CL4").Value

Do While Not EOF(1)
    Line Input #1, strLine
    If InStr(strLine, strLine_old_head) Then
        Line Input #1, strLine: Print #2, strLine_new_head
        Line Input #1, strLine: Print #2, strLine_new_1
        Line Input #1, strLine: Print #2, strLine_new_2
        Line Input #1, strLine: Print #2, strLine_new_3
    Else
        Print #2, strLine
    End If
Loop

Close #1
Close #2

FileCopy strNewFlowfile, strFlowfile
Kill strNewFlowfile
HRC.QuitRas

Sheet2.Range("CK2") = Sheet2.Range("CJ2").Value 'NILAI SEBELUMNYA

End Sub

问题分析

错误的核心是:当代码匹配到目标行strLine_old_head后,会连续执行4次Line Input #1读取后续行,但此时文件可能已经没有足够的剩余行数,导致读取超出文件末尾,触发Error 62。

可能的触发场景:

  • .U04文件结构发生变化(比如末尾内容被截断、匹配行之后的行数不足)
  • 文件意外损坏,导致行数缺失
  • strLine_old_head的匹配位置出现在文件最后几行,后续没有足够行供读取

修复方案

在每次执行Line Input前检查是否已到文件末尾,避免越界读取。修改后的关键代码如下:

Do While Not EOF(1)
    Line Input #1, strLine
    If InStr(strLine, strLine_old_head) Then
        ' 每次读取前检查是否已到文件末尾
        If Not EOF(1) Then
            Line Input #1, strLine: Print #2, strLine_new_head
        End If
        If Not EOF(1) Then
            Line Input #1, strLine: Print #2, strLine_new_1
        End If
        If Not EOF(1) Then
            Line Input #1, strLine: Print #2, strLine_new_2
        End If
        If Not EOF(1) Then
            Line Input #1, strLine: Print #2, strLine_new_3
        End If
    Else
        Print #2, strLine
    End If
Loop

额外优化建议

  1. 增加错误捕获:在文件操作前后添加错误处理,避免程序崩溃并快速定位问题:
On Error GoTo ErrorHandler
' 原文件操作代码...
Exit Sub
ErrorHandler:
    MsgBox "错误编号: " & Err.Number & vbCrLf & "错误描述: " & Err.Description
    Close #1
    Close #2
    HRC.QuitRas
  1. 文件备份:在覆盖原.U04文件前,先备份一份,防止文件损坏无法恢复:
FileCopy strFlowfile, strFlowfile & ".bak" ' 备份原文件
FileCopy strNewFlowfile, strFlowfile
  1. 验证文件完整性:处理前先检查.U04文件的总行数,确保匹配行之后有足够行数供读取。

内容的提问来源于stack exchange,提问作者Muhammad Kafiansyah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 06:47:36