gpt4 book ai didi

vba - Excel VBA : Error Handling Breaking Mid-Code

转载 作者:行者123 更新时间:2023-12-03 08:03:30 28 4
gpt4 key购买 nike

代码遍历大量数据以执行计算。非常适合前两个工作簿。第三本工作簿,突然出现错误处理中断-不再起作用。关于为什么的想法?

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

提前致谢。

最佳答案

您可以通过这种方式进行操作并管理运行时错误:

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

...或者无需管理运行时错误,只需删除 WorksheetFunction并测试错误的函数返回值即可:
Dim m
m = Application.MRound(currentminute - oldminute, 1 / 86400)
If IsError(m) Then
m = Application.MRound((-1 * (currentminute - oldminute)), 1 / 86400)
End If

关于vba - Excel VBA : Error Handling Breaking Mid-Code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51599414/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com