VBA 错误处理 - 给定情况的最佳实践是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12711286/
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 17:57:56  来源:igfitidea点击:

VBA error handling - what are the best practices for the given situation?

excel-vbavbaexcel

提问by Siraj Samsudeen

I have a program which opens many Excel workbooks and merges data from all of them into a target workbook. After merging, it does some transformation of the data (column to row tranpose using custom logic) and then it adds some more columns using lookup data.

我有一个程序可以打开许多 Excel 工作簿并将所有这些工作簿中的数据合并到一个目标工作簿中。合并后,它对数据进行一些转换(使用自定义逻辑的列到行转置),然后使用查找数据添加更多列。

I have the following code

我有以下代码

Sub consolidateFiles()
    On Error GoTO ErrorHandler

    processFiles
    transform
    addLookupData

    Exit Sub
    ErrorHandler:
    Debug.Print Err.Number & " = " & Err.Description & ", Source = " & Err.Source 
End Sub

Sub processFiles()
 'Here I open all the files in the directory and copy and paste into the target workbook. 
 'If there is an error here, I would like to know the file that caused the error 
 'and I would like to proceed to the next file after logging the error
End Sub

Sub transform()
 'Here I transform some of the data from column to year.
End Sub

Sub addLookupData()
 'Here I add some new columns by looking up the data in another sheet in the target  workbook. Here the lookups can return error
End Sub

I wanted to know what are the good programming practices to add error handling to my code. Right now, I have one global error handler which logs the error and continues the program. Is it a good idea to add error handling to each of the 3 sub-procedures that I am calling from my main procedure?

我想知道在我的代码中添加错误处理的良好编程实践是什么。现在,我有一个全局错误处理程序,它记录错误并继续执行程序。为我从主过程调用的 3 个子过程中的每一个添加错误处理是否是个好主意?

I read about using the undocumented ERL to get the line of error. How do we add line numbers in the VBA editor?

我阅读了有关使用未记录的 ERL 来获取错误行的信息。我们如何在 VBA 编辑器中添加行号?

This is my first program in VBA. I have done a lot of Java programming, but there the error handling paradigm is very different. Hence, I would like to learn from experienced VBA programmers what are the good practices that I can follow.

这是我在 VBA 中的第一个程序。我做过很多 Java 编程,但是那里的错误处理范例非常不同。因此,我想向经验丰富的 VBA 程序员学习我可以遵循的良好实践。

I have read this excellent article which was linked in one of the questions related to VBA error handling in SO:

我已经阅读了这篇出色的文章,该文章链接在与 SO 中 VBA 错误处理相关的问题之一中:

http://www.cpearson.com/excel/errorhandling.htm

http://www.cpearson.com/excel/errorhandling.htm

采纳答案by Siraj Samsudeen

Since I did not get any answers, I have tried to research more. Here are some article that I have found useful:

由于我没有得到任何答案,我试图进行更多研究。以下是我发现有用的一些文章:

http://www.techrepublic.com/blog/five-apps/five-tips-for-handling-errors-in-vba/339

http://www.techrepublic.com/blog/five-apps/five-tips-for-handling-errors-in-vba/339

http://www.jpsoftwaretech.com/vba-tips-tricks-and-best-practices-part-four/

http://www.jpsoftwaretech.com/vba-tips-tricks-and-best-practices-part-four/

http://www.fmsinc.com/tpapers/vbacode/debug.asp#BasicErrorHandling

http://www.fmsinc.com/tpapers/vbacode/debug.asp#BasicErrorHandling

Also, I have found following a SingleExitPoint in each procedure (either with error or without error) as a good practice.

此外,我发现在每个过程中遵循 SingleExitPoint(有错误或没有错误)是一种很好的做法。

Many of the ideas are stated/hinted at in the link that Siddharth provided. But they were not very clear to me until I read these articles.

许多想法在 Siddharth 提供的链接中陈述/暗示。但是直到我阅读这些文章之前,它们对我来说并不是很清楚。