Excel VBA全局错误处理 [英] Excel VBA Global error handling
问题描述
我可以在Workbook代码中放置一些代码,以捕获所有模块中发生的任何错误吗? p>
我可以在每个模块中放置相同的错误处理程序,但我正在寻找更一般的东西。
我问因为我有将这些名称存储为全局变量,如 Sheets(QuoteName)
。如果有错误,那么这些全局变量将丢失。我有一个宏,它将重命名全局变量,但我把它放在 Workbook_BeforeSave
中。
我想要它到全局错误处理程序并重命名全局变量,如果我得到表(QuoteName)
由于Sid在注释中已经提到,没有中心错误处理程序。
实践是有一个从本地错误处理程序调用的中心错误处理例程。看看伟大的 MZ-Tools :它有可能定义一个默认错误处理程序按下按钮( Ctrl - E )。您可以自定义此错误处理程序 - 它还可以包含模块和/或子名称!
此外,请查看此文章 Excel的每日剂量。这是Dick Kusleika的OO版本的错误处理程序在这本书中提出的(我可以高度的推荐)。
Is there a way to do global error handling?
Can I put some code in the Workbook code that will catch any errors that occur within all modules?
I could put the same error handler in each module but I'm looking for something more general.
I ask because I have sheet names that are stored as global variables like this Sheets(QuoteName)
. If there is an error then these global variables are lost. I have a macro that will rename the global variables but I put this within Workbook_BeforeSave
.
I want it to go to the global error handler and rename the global variable if I get a Subscript out of range error for Sheets(QuoteName)
As Sid already mentioned in the comment, there is no central error handler.
Best practice is to have a central error handling routine that gets called from the local error handlers. Take a look at the great MZ-Tools: it has the possibility to define a default error handler at the press of a button (Ctrl-E). You can customize this error handler - and it can also contain module and/or sub name!
Additionally, check out this post at Daily Dose of Excel. It is Dick Kusleika's OO version of the error handler proposed in this book (which I can highly recommend).
这篇关于Excel VBA全局错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!