所以我创建了一个相当大的宏,它为我的公司创建了 PowerPoint 演示文稿。我希望能够使用以下方法为 2 个不同的区域运行它:
For each sRegion
MyMacro(sRegion)
Next
Run Code Online (Sandbox Code Playgroud)
有几行我想跳过。对于第一次运行,MyMacro 运行良好,错误处理程序会跳过这些步骤。但是对于下一个 sRegion 错误处理程序不起作用。
即使我逐行通过 on error resume next 语句,它也不起作用,实际上只是停止了宏。
我会在它中断的地方发布代码,尽管它完全无关(在第二次运行时,当首先调用宏时工作正常)
On Error Resume Next
PPPres.Slides(19).Moveto ToPos:=12
PPPres.Slides(20).Moveto ToPos:=13
PPPres.Slides(21).Moveto ToPos:=14
PPPres.Slides(22).Moveto ToPos:=15
PPPres.Slides(23).Moveto ToPos:=16
On Error GoTo 0
Run Code Online (Sandbox Code Playgroud)
它将完全忽略 on 错误并抛出错误并停止宏。
在有人建议是之前,我已经检查了错误捕获是否处于“中断未处理的错误”状态,并且它是
任何人以前遇到过这个问题或知道解决方案吗?
确保在跳转错误(您使用On Error GoTo)之后,您使用ResumeorResume Next或Resume <label>命令删除错误条件。或者,删除那个On Error GoTo,只使用On Error Resume Next.
根据您的问题和评论,您正在执行以下操作,什么总是在第二个语句中引发错误:
Sub WrongOne()
On Error Goto Handler1 'prepared for error
Statement1WithError() 'error causes jump
Handler1: 'jump done, error handling is now disabled
Statement2WithError() 'THIS WILL ALWAYS THROW ERROR
End Sub
Run Code Online (Sandbox Code Playgroud)
你总是会出错 Statement2WithError()
正确做法:
Sub CorrectOne()
On Error Goto Handler1 '1. prepared for error
Statement1WithError() '2. error causes jump
Waypoint1: '5. continuing here
On Error Goto Handler2 '6. prepared for another error
Statement2WithError() '7. error causes jump to Handler2
Statement3WithError() '10. error causes jump to Handler2
Statement4WithError() 'etc...
Exit Sub
'EXAMPLE: after error, continue at SPECIFIC LABEL
Handler1: '3. jump done, error handling is now disabled
MsgBox(...)
Resume Waypoint1 '4. error handling is reloaded, jumping to label
'EXAMPLE: after error, continue with NEXT LINE
Handler2: '8. jump done, error handling is now disabled
MsgBox(...)
Resume Next '9. error handling is reloaded, now jumping to line
' following the line that caused error
End Sub
Run Code Online (Sandbox Code Playgroud)
在您的情况下,VBA 按预期工作。您可以研究错误处理在 VBA 中的工作原理。
| 归档时间: |
|
| 查看次数: |
4263 次 |
| 最近记录: |