Excel VBA 错误 GoTo 未按预期工作
Excel VBA On Error GoTo not working as intended
我正在使用 On Error GoTo 来捕获我的函数 Linterp, 的错误,它接受一个单元格和两个范围。有时,Linterp 会抛出一个错误,在这种情况下,我只想将单元格设置为 5。然而,其他时候,当 Linterp 没有抛出错误并正确 returns 一个预期的数字时,例如3.25 或类似的东西,函数仍然转到“案例 1”并将单元格设置回 5。
此外,尽管 this suggestion 我在 On Error Goto 之后插入了一个 Exit Sub,但我不认为我可以在我的特定情况下这样做,因为我希望函数继续遍历每个单元格,即使其中一个单元在第一次尝试时就正确执行了 Linterp。
Sub Linterp_Test_1()
For Each cell In Selection
Set cell_index = Cells(3, "I")
Set cell_xs = Range(Cells(cell.Row, "K"), Cells(cell.Row, "O"))
Set cell_ys = Range(Cells(cell.Row, "D"), Cells(cell.Row, "H"))
On Error GoTo Case1
cell.Value = Linterp(cell_index, cell_xs, cell_ys)
Resume Next
Case1:
cell.Value = 5
Resume Next
Next cell
End Sub
错误处理
我更喜欢你的第一个解决方案。
Option Explicit
Sub Linterp_Test_1()
If TypeName(Selection) <> "Range" Then Exit Sub
On Error Resume Next
For Each cell In Selection
Set cell_index = Cells(3, "I")
Set cell_xs = Range(Cells(cell.Row, "K"), Cells(cell.Row, "O"))
Set cell_ys = Range(Cells(cell.Row, "D"), Cells(cell.Row, "H"))
cell.Value = Linterp(cell_index, cell_xs, cell_ys)
If Err.Number <> 0 Then
cell.Value = 5
Err.Clear
End If
Next cell
On Error GoTo 0
End Sub
Sub Linterp_Test_2()
If TypeName(Selection) <> "Range" Then Exit Sub
For Each cell In Selection
Set cell_index = Cells(3, "I")
Set cell_xs = Range(Cells(cell.Row, "K"), Cells(cell.Row, "O"))
Set cell_ys = Range(Cells(cell.Row, "D"), Cells(cell.Row, "H"))
On Error Resume Next
cell.Value = Linterp(cell_index, cell_xs, cell_ys)
If Err.Number <> 0 Then cell.Value = 5
On Error GoTo 0
Next cell
End Sub
我正在使用 On Error GoTo 来捕获我的函数 Linterp, 的错误,它接受一个单元格和两个范围。有时,Linterp 会抛出一个错误,在这种情况下,我只想将单元格设置为 5。然而,其他时候,当 Linterp 没有抛出错误并正确 returns 一个预期的数字时,例如3.25 或类似的东西,函数仍然转到“案例 1”并将单元格设置回 5。
此外,尽管 this suggestion 我在 On Error Goto 之后插入了一个 Exit Sub,但我不认为我可以在我的特定情况下这样做,因为我希望函数继续遍历每个单元格,即使其中一个单元在第一次尝试时就正确执行了 Linterp。
Sub Linterp_Test_1()
For Each cell In Selection
Set cell_index = Cells(3, "I")
Set cell_xs = Range(Cells(cell.Row, "K"), Cells(cell.Row, "O"))
Set cell_ys = Range(Cells(cell.Row, "D"), Cells(cell.Row, "H"))
On Error GoTo Case1
cell.Value = Linterp(cell_index, cell_xs, cell_ys)
Resume Next
Case1:
cell.Value = 5
Resume Next
Next cell
End Sub
错误处理
我更喜欢你的第一个解决方案。
Option Explicit
Sub Linterp_Test_1()
If TypeName(Selection) <> "Range" Then Exit Sub
On Error Resume Next
For Each cell In Selection
Set cell_index = Cells(3, "I")
Set cell_xs = Range(Cells(cell.Row, "K"), Cells(cell.Row, "O"))
Set cell_ys = Range(Cells(cell.Row, "D"), Cells(cell.Row, "H"))
cell.Value = Linterp(cell_index, cell_xs, cell_ys)
If Err.Number <> 0 Then
cell.Value = 5
Err.Clear
End If
Next cell
On Error GoTo 0
End Sub
Sub Linterp_Test_2()
If TypeName(Selection) <> "Range" Then Exit Sub
For Each cell In Selection
Set cell_index = Cells(3, "I")
Set cell_xs = Range(Cells(cell.Row, "K"), Cells(cell.Row, "O"))
Set cell_ys = Range(Cells(cell.Row, "D"), Cells(cell.Row, "H"))
On Error Resume Next
cell.Value = Linterp(cell_index, cell_xs, cell_ys)
If Err.Number <> 0 Then cell.Value = 5
On Error GoTo 0
Next cell
End Sub