Excel VBA:错误处理打破中间代码

R Q*_*ade 0 error-handling excel vba excel-vba

代码循环执行大量执行计算的数据.适用于前两本工作簿.第三个工作簿,突然错误处理中断 - 不再有效.想法为什么?

1) Break on Unhandled Errors在选项中正确标记

2)每个错误处理之后是On Error GoTo 0

3)这打开On Error Resume Next和On Error GoTo ErrHandler.

我以为OERN无视任何其他错误处理?

这是冗长的代码.我拿出了几个变量定义来缩短它.

amount = lastcolumn / 6
totalstrikes = 0
Do Until amount = 0
    currentcolumn = amount * 6 - 5
    i = 2
Do Until Sheets("Data").Cells(i, currentcolumn).Value = ""
    currentminute = Sheets("Data").Cells(i, currentcolumn).Value
    If oldminute <> 0 Then
    On Error GoTo ErrHandler
        If WorksheetFunction.MRound(currentminute - oldminute, 1 / 86400) >= 0.0007 Then
            'Do Stuff
        End If
5        End If
    On Error GoTo 0
        Do Until Sheets("Data").Cells(i, currentcolumn) <> currentminute
        If InStr(1, hitlist, Sheets("Data").Cells(i, currentcolumn + 1).Value) = False Then
            totaltime = totaltime + CSng(Sheets("Data").Cells(i, currentcolumn + 4).Value)
            totaltotal = totaltotal + CSng(Sheets("Data").Cells(i, currentcolumn + 2).Value)
        End If
        i = i + 1
    Loop
    On Error Resume Next
    If totaltime / totaltotal <= failuretime Then
        Strike = 1
    Else
        Strike = 2
    End If
    On Error GoTo 0
    If minute1 = 0 Then
        'do stuff with the minutes
    End If
    oldminute = currentminute
Loop
amount = amount - 1
Loop
Exit Sub
ErrHandler:
If WorksheetFunction.MRound((-1 * (currentminute - oldminute)), 1 / 86400) >= 0.0007 Then
    Resume Next
Else
    GoTo 5
End If
End Sub
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Tim*_*ams 6

您可以这样做并管理运行时错误:

If WorksheetFunction.MRound(currentminute - oldminute, 1 / 86400) >= 0.0007 Then
    'Do Stuff
End If
Run Code Online (Sandbox Code Playgroud)

...或者不需要管理运行时错误,通过删除WorksheetFunction并测试函数的错误返回值:

Dim m
m = Application.MRound(currentminute - oldminute, 1 / 86400)
If IsError(m) Then
    m = Application.MRound((-1 * (currentminute - oldminute)), 1 / 86400)
End If
Run Code Online (Sandbox Code Playgroud)