仅在特定范围内应用“出错转到”

我只想为代码中的下几行应用“On Error GoTo err1”

On Error GoTo err1 ActiveWorkbook.SaveAs Filename:="C:\project\" & Year(Date) & "\" & _ MonthName(Month(Date)) & "\" & MyFileName & ".xls" ActiveWorkbook.Close SaveChanges:=False 

就是这样,之后我想禁用error handling。 我试图添加“On Error resume”,但没有任何结果。 err1处理看起来像这样:

  err1: MsgBox ("Project will not overwrite the file"), vbCritical ActiveWorkbook.Close SaveChanges:=False 

该子文件将一个文件保存到新的.xls文件中,并按date自动保存。 如果已经生成了一个文件,当被要求覆盖文件时,一个运行macros的人点击“否”,这个err1会popup消息,并且会阻止用户获得debugging消息。 但是,这个error handling似乎是通过所有的代码是不好的 – 在保存文件子产生一个自动电子邮件,如果该人想要返回到项目之前点击“发送”button,它去相同的err1并closures原来的工作簿(不是第一次发生的单独表格),并使用户无语。 我想避免这样的情况,因为具有不良的Excel技能的用户会做出这些报告。

有什么build议么?

这实际上不是一个答案,但我不能适应它的评论。

我假设你做了如下的事情。 所以你有点夹心你的怀疑行产生错误与OEG和OEG0如下:

 Sub test() On Error GoTo err1 'Access non existing named range Range("ProduceError").Select 'Above will produce error and your handler kicks in On Error GoTo 0 MsgBox "First error handled successfully" 'Access invalid range address Range("B0").Select 'Above will error out normally and err1 not called 'OEG<Label> only called once and not in the entire macro Exit Sub err1: MsgBox Err.Description Resume Next End Sub 

现在你看,你的error handling程序在OEG0行之后不会再被调用。
遇到第二个错误生成行后,将会出现正常错误。
现在,您可能需要检查这个来查看error handling 。