Excel:错误中断在用户定义函数中不起作用
似乎当从单元格公式(即“用户定义函数”或 UDF)调用 VBA 代码时,通常的 VBA Break On Error 不起作用。
我唯一能找到记录此行为的地方是一篇名为 “为 Excel 2007 和 Excel Services 开发用户定义的函数”:
错误处理返回 #VALUE 错误。 UDF 代码引发的所有异常都会作为 #VALUE 错误返回到 Excel 工作表中。
即使您将错误捕获设置为“出现所有错误时中断”并单步执行代码**,您也永远不会看到 VBA 运行时错误对话框 - Excel 只是悄悄地放弃执行,而不告诉您出了什么问题。当然,这使得调试变得比实际需要的更加困难。
有一些涉及On Error
的潜在解决方法,但我不想为了找出引发错误的位置而弄乱我的代码。
是否有一些我忽略的 Excel/VBA 选项可以使“错误中断”正常工作?我使用的是 Excel 2003。
** 从单元格调用时进入调试器的唯一方法是设置断点或使用 Stop
语句
It seems that when VBA code is called from a cell formula (ie. a "User Defined Function" or UDF), the usual VBA Break On Error doesn't work.
The only place I can find this behavior documented is a couple of lines in an article called "Developing User-Defined Functions for Excel 2007 and Excel Services":
Error handling returns #VALUE errors. All exceptions thrown by the UDF code are returned into the Excel worksheet as #VALUE errors.
Even if you set Error Trapping to "Break on All Errors" and single-step your code**, you will never see the VBA Run-time Error dialog - Excel just quietly abandons execution without telling you what went wrong. Of course this makes debugging more difficult than it needs to be.
There are some potential workarounds involving On Error
but I'd rather not clutter up my code just to find out where an error was raised.
Is there some Excel / VBA option I've overlooked which will make Break On Error work normally? I'm using Excel 2003.
** The only way to get into the debugger when called from a cell is to set a breakpoint or use a Stop
statement
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最好的方法是使用
On Error GoTo ErrorHandler
以及Stop
引用,后跟Resume
。需要小心,不要进入
resume
的无限循环,因为 UDF 几乎连续运行(如果发生这种情况,请重复按Esc
),因此在代码中添加:
On错误 GoTo ErrorHandler
靠近函数的开头,然后在End Function
之前的末尾:Exit Function
会停止此代码在正常操作中运行。如果遇到错误,将弹出包含详细信息的消息框,代码将中断(由于
Stop
),您可以单步返回代码(通过resume
跳回) code> 语句)使用调试工具栏上的下一行命令。当然,当您对 UDF 感到满意时,请不要忘记注释掉
On Error GoTo ErrorHandler
行。Best method would be to use the
On Error GoTo ErrorHandler
with aStop
reference followed byResume
.Need to be careful not to get into an infinite loop with
resume
as UDFs run almost continually (if this happens hitEsc
repeatedly)So in your code add:
On Error GoTo ErrorHandler
near the start of your function and then right at the end beforeEnd Function
:The
Exit Function
stops this code running in normal operation.If an error is encountered a messagebox with the details will pop up, the code will break (due to
Stop
) and you can step through back into your code (hopping back via theresume
statement) using the next line command on the debug toolbar.Of course don't forget to comment out the
On Error GoTo ErrorHandler
line when you're happy with your UDF.我知道,当您特别要求
On Error
之外的其他内容时,听到这个消息并不有趣,但据我所知,这恐怕是唯一的选择。您可以在调试时使用
On Error Goto ErrorHandler
(并在其他时候将其注释掉以获取默认的On Error Goto 0
)。ErrorHandler
可以只有几行,这样就不会让您的代码变得过于混乱:始终在
Resume
上设置一个断点,以引导您回到导致错误的地方单步执行时的语句——并避免无限的错误处理循环。I'm aware that it isn't any fun hearing this when you specifically asked for something else than
On Error
, but I'm afraid it's the only option to my knowledge.You could just use
On Error Goto ErrorHandler
while debugging (and comment it out to get the defaultOn Error Goto 0
at other times). TheErrorHandler
could have just a couple of lines so it doesn't clutter up your code too much:always with a breakpoint on
Resume
to guide you back to the error-causing statement upon stepping -- and to avoid an infinite error-handling loop.我最近也遇到了这个。我真的以为有些东西已经改变了,而且并不总是这样 - 但我想我错了。
以下可能是对所建议内容的一个小改进。我添加了一个调试?提示消息框,以便您可以调试或不调试。所以没有无限循环。导致错误的行会突出显示。
假设这是我原来的功能:
如果我怀疑有问题,那么我将添加如下三行:
然后,当发生错误时:
screenshot
这有点麻烦,但也不算太糟糕。如果在调试时发生错误,那没有问题。您只需按 F8、YES、F8、F8 即可返回。
请注意,错误捕获必须设置为“未处理错误时中断”或“类模块中中断”。
I ran into this recently also. I really thought something had changed and it didn't always work like this -but I guess I was wrong.
Below is a perhaps a small improvement on what has been suggested. I added a Debug? prompt to the message box so that you can either debug or not. So no infinite loops. And the line causing the error is highlighted.
Say this is my original function:
If I suspect a problem, then I'll add three lines like this:
Then when an error happens:
screenshot
This is some trouble, but not too bad. If an error happens while you're debugging, it's no problem. You just hit F8, YES, F8, F8 to get to back in.
Note that Error Trapping must be set to Break on Unhandled Errors, or Break in Class Module.