VBA 错误处理在 Excel 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3440735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:59:02  来源:igfitidea点击:

VBA Error Handling not working in Excel

excelvba

提问by Samuel Slade

I have not had much experience with VBA, but I sometimes use it at work. Recently, I have encountered a problem that shouldn't happen, and that neither my boss nor myself can figure out.

我对 VBA 没有太多经验,但我有时会在工作中使用它。最近,我遇到了一个不应该发生的问题,我的老板和我自己都想不通。

Basically, the issue is that the Applicationproperty DisplayAlertsis set to Trueby default and can't be changed for some reason. Possibly related is that when I hit an error, it always display the End|Debug|Help alert and never hits the applied error handling.

基本上,问题在于该Application属性默认DisplayAlerts设置为True,并且由于某种原因无法更改。可能相关的是,当我遇到错误时,它总是显示 End|Debug|Help 警报并且从不命中应用的错误处理。

I am running 64-bit Office 2010 on a 64-bit Windows 7 machine. However, I do not believe it to be a platform issue, as I have tested on multiple different platforms, operating systems and software permutations and no other machine has this error; just mine.

我在 64 位 Windows 7 机器上运行 64 位 Office 2010。但是,我不认为这是平台问题,因为我已经在多个不同的平台、操作系统和软件排列上进行了测试,并且没有其他机器出现此错误;只是我的。

I have created some sample code in case anyone has encountered this before or has any ideas. The only thing I can think of, is that I have something installed on my machine that is causing this. But after a program purge and many restarts, I am no closer to deciphering what it might be.

我已经创建了一些示例代码,以防有人以前遇到过这个问题或有任何想法。我唯一能想到的是,我的机器上安装了导致这种情况的东西。但是在程序清除和许多重新启动之后,我离破译它可能是什么更近了。

Public Sub TestErrorHandler()

    ' Suppress alerts
    Application.DisplayAlerts = False

    Dim strArray(1) As String
    strArray(0) = "Hello"
    strArray(1) = "World"

    ' Set up error handler
    On Error GoTo ErrHandler

    For i = 0 To 3
        MsgBox strArray(i)
    Next

    ' Strip the error handler
    On Error GoTo 0

    ' Unsuppress alerts
    Application.DisplayAlerts = True

    Exit Sub

    ErrHandler:

    MsgBox "Error: " & Err.Description

    Resume Next

End Sub

The error is thrown on the third enumeration of the for-loop(as it should). The type of the error is irrelevant, what is relevant is that I get the error and never hit the error handler.

错误在第三次枚举时抛出for-loop(应该如此)。错误的类型无关紧要,相关的是我收到错误并且从未命中错误处理程序。

Any suggestions or help on this would be greatly appreciated.

对此的任何建议或帮助将不胜感激。

Many thanks!

非常感谢!

回答by shahkalpesh

Press ALT + F11from within Excel to reach VBA editor.

ALT + F11在 Excel 中按以访问 VBA 编辑器。

Goto Tools menu -> Options item -> General tab.

转到工具菜单 -> 选项项 -> 常规选项卡。

Set the error trapping to "Break on unhandled errors"

将错误捕获设置为“中断未处理的错误”


(source: microsoft.com)


(来源:microsoft.com