在错误恢复下一个似乎不工作 [英] On Error Resume Next seemingly not working

查看:104
本文介绍了在错误恢复下一个似乎不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两行代码:

On Error Resume Next
myWorkbook.Sheets("x").Columns("D:T").AutoFit

我已经进入了宏执行行 On Error Resume Next ,然后在下一行 myWorkbook ... 中执行以下操作:

I've stepped into the macro and executed the line On Error Resume Next and then on the next line myWorkbook... it does the following:

为什么编译器不能继续下一行代码?

Why doesn't the compiler resume the next line of code?

On错误在整个程序代码中已被广泛使用;我认识到最好的做法是尽可能少地使用它,但它似乎符合这个宏的目的。

On Error has been liberally used throughout the procedures code; I realize best practice is to use this as little as possible but it seems to fit the purpose of this macro.

阅读这个 SO QUESTION 它说你不能有一组错误陷阱在另一个之内。在代码移动之前,我如何保证一组错误陷阱已经被关闭了 - On Error Goto 0 重置错误陷阱?如果它重置,那么为什么不恢复以下工作?:

Reading this SO QUESTION it says that you can't have one set of error trapping within another. How can I guarantee that one set of error trapping has been "closed off" before the code moves on - does On Error Goto 0 reset the error trapping? If it does reset then why doesn't resume work in the following?:

Sub GetAction()
Dim WB As Workbook
Set WB = ThisWorkbook

On Error GoTo endbit:
'raise an error
Err.Raise 69
Exit Sub
endbit:
On Error GoTo 0

On Error Resume Next
WB.Sheets("x").Columns("D:T").AutoFit

End Sub


推荐答案

初始错误未关闭。

Sub GetAction()
Dim WB As Workbook
Set WB = ThisWorkbook
On Error GoTo endbit:
'raise an error
Err.Raise 69
Exit Sub
endbit:
On Error Resume Next
WB.Sheets("x").Columns("D:T").AutoFit
End Sub

这篇关于在错误恢复下一个似乎不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆