【发布时间】:2020-11-19 19:23:58
【问题描述】:
我正在使用 On Error GoTo 来捕捉我的函数 Linterp, 的错误,该函数包含一个单元格和两个范围。有时,Linterp 会抛出错误,在这种情况下,我只想将单元格设置为 5。但是,其他时候,当 Linterp 没有抛出错误并正确返回预期数字时,例如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
【问题讨论】:
-
发生错误时需要清除错误。您可能想查看THIS 您也不需要使用
GoTo Case1。检查该帖子中On Error Resume Next如何与If Err.Number <> 0 Then一起使用... -
如果你能发布
Linterp函数的代码就好了,因为使用范围不是很有效。
标签: excel vba error-handling goto