VBAerror handling查询

我希望有人可以看看,为我整理这个; 我不得不说error handling不是我的强点。 我有下面的代码块,我一直玩弄一些错误句柄,但它不是我真正想要的。

我所要做的是确保如果任何时候出现错误,我已经打开的工作簿和excel实例将优雅地closures。

我确信有比我所提出的更好,更简单的方法来达到这个目标。

Sub QOScode() On Error GoTo Fail Dim app As New Excel.Application app.Visible = False 'Visible is False by default, so this isn't necessary Dim book As Excel.Workbook Set book = app.Workbooks.Add(ActiveWorkbook.Path & "\QOS DGL stuff.xlsx") 'set up error handeling so if any thing happens the instance of excel with QOS sheets is closed gracefully On Error GoTo Closebook ' MsgBox book.Sheets("ACLS").Cells(3, 3) 'Do what you have to do ' Closebook: On Error Resume Next book.Close SaveChanges:=False app.Quit Set app = Nothing On Error GoTo 0 Fail: End Sub 

我想要的是一个单一的错误 – closures应用程序和退出子。

任何人都可以提供一个什么样的最好的做法吗?

干杯

亚伦

所以下面的代码,当表单不存在的时候会导致错误,为什么不跳过“ book.close ”语句,我知道这会抛出一个错误,但是我希望它忽略它呢?

 Sub QOScode() On Error GoTo Closebook Dim app As New Excel.Application app.Visible = False Dim book As Excel.Workbook Set book = app.Workbooks.Add(ActiveWorkbook.Path & "\QOaS DGL stuff.xlsx") 'this sheet does not exist ' MsgBox book.Sheets("ACLS").Cells(3, 3) 'Do what you have to do ' Closebook: Err.Clear On Error Resume Next book.Close SaveChanges:=False 'Object variable or with block variable not set (error 91) app.Quit Set app = Nothing On Error GoTo 0 End Sub 

我的error handling2美分。

应该总是做error handling。

一些原因

1)你不会希望你的应用程序崩溃,让你的用户挂起! 想象一下,它会导致他们的挫折。

2)error handling并不意味着你试图忽略错误。

3)error handling既不是防御性的编程,也不是积极的编程。 恕我直言,这是积极的编程。

4)很less有人知道你可以找出导致错误的线。 我谈论的财产是ERL。 考虑这个例子

 Sub Sample() Dim i As Long Dim j As Long, k As Long 10 On Error GoTo Whoa 20 i = 5 30 j = "Sid" 40 k = i * j 50 MsgBox k 60 Exit Sub Whoa: 70 MsgBox "Description : " & Err.Description & vbNewLine & _ "Error Number : " & Err.Number & vbNewLine & _ "Error at Line: " & Erl End Sub 

在这里输入图像说明

5)在工作表变更事件这样的子工作中,必须进行error handling。 想象一下,你已经把启用事件设置为假,你的代码打破了! 该代码将不会运行,直到您将事件设置为true

6)我可以继续:-)会推荐这个链接

主题:“Err”是人类

链接 : http : //www.siddharthrout.com/2011/08/01/to-err-is-human/

小费:

使用MZ工具 。 这是免费的!

这是我将如何写你的代码。

 Sub QOScode() Dim app As New Excel.Application Dim book As Excel.Workbook 10 On Error GoTo Whoa 20 Set book = app.Workbooks.Open(ActiveWorkbook.Path & "\QOS DGL stuff.xlsx") 30 MsgBox book.Sheets("ACLS").Cells(3, 3) ' 'Do what you have to do ' LetsContinue: 40 On Error Resume Next 50 book.Close SaveChanges:=False 60 Set book = Nothing 70 app.Quit 80 Set app = Nothing 90 On Error GoTo 0 100 Exit Sub Whoa: 110 MsgBox "Description : " & Err.Description & vbNewLine & _ "Error Number : " & Err.Number & vbNewLine & _ "Error at Line: " & Erl 120 Resume LetsContinue End Sub 

我不太清楚我的理解你的目标。 如果我这样做,我可能会不同意。

这是你正在开发的代码? 我几乎从来没有在我正在开发的代码中使用error handling。 我希望解释者停止在发生错误的陈述上。 我想了解为什么发生这个错误。 我能做些什么来避免这个错误? 我没有检查文件是否存在? 我没有检查path是否可访问? 我会在做其他事情之前添加缺less的代码。

当你完成开发时,你应该计划没有错误条件,你没有包括正确的代码。 当然,这是不可能的。 你不能使你的代码万无一失,因为愚人是如此的巧妙。 您发布给用户的版本必须包含error handling。

但是你不能把这个代码发布给用户,因为它会在没有警告的情况下停止。 用户会猜测macros是否有问题,或者他们会认为这是应该发生的事情? 如果他们认为macros观失败了,他们会对你说什么? “这不是我所期待的,我不知道为什么。” 你打算说什么? “你在做什么?” 我不认为我曾经有一个用户给出了一个可信的描述,他们在失败的时候正在做什么。 至less你想要:

 Call MsgBox("Sorry I have had an unrecoverable error within QOScode()." & _ " Please record: " & Err.Number & " " & Err.Description & _ " and report to extension 1234") 

有了这个,用户不会怀疑是否有什么问题,而且你知道哪里出了问题,幸运的话,为什么。

要巧妙地处理预见的问题,你可以使用短的error handling来testing工作簿是否真实存在(即If Not Wb Is Nothing Then ,那么如果是这样的话,以一个共同的结局(即销毁对象)

第二个示例演示如何在工作簿打开后,为不可预见的错误添加其他处理。我使用了Err.Raise来创build故意错误,以便用户select如何继续(在错误后立即closures工作簿使工作簿可见)

另外,不要一起使用DimNew 。 我已经重写了
Dim app As New Excel.Application

Dim xlApp As Excel.Application
Set xlApp = New Excel.Application

1.处理没有工作簿的问题

 Sub QOScode() Dim xlApp As Excel.Application Set xlApp = New Excel.Application Dim Wb As Excel.Workbook On Error Resume Next Set Wb = xlApp.Workbooks.Add(ActiveWorkbook.Path & "\QOaS DGL stuff.xlsx") 'this sheet does not exist On Error GoTo 0 ' If Not Wb Is Nothing Then MsgBox Wb.Sheets("ACLS").Cells(3, 3) 'Do what you have to do Wb.Close False End If xlApp.Quit Set xlApp = Nothing End Sub 

2.处理没有工作簿和其他不可预知的错误,否则可能会使工作簿打开

 Sub QOScode2() Dim xlApp As Excel.Application Set xlApp = New Excel.Application Dim Wb As Excel.Workbook Dim lngChk As Long On Error Resume Next Set Wb = xlApp.Workbooks.Add(ActiveWorkbook.Path & "\QOaS DGL stuff.xlsx") 'this sheet does not exist On Error GoTo 0 ' If Not Wb Is Nothing Then On Error GoTo ProblemHandler MsgBox Wb.Sheets("ACLS").Cells(3, 3) 'Do what you have to do 'Deliberate error Err.Raise 2000, "test code", "Sample Error" Wb.Close False Else MsgBox "Workbook not found,code will now exit" End If xlApp.Quit Set xlApp = Nothing Exit Sub ProblemHandler: 'Test to see if workbook was still open when error happened If Not Wb Is Nothing Then lngChk = MsgBox("The code encountered an error" & vbNewLine & Err.Number & vbNewLine & "Do you want to close the file?", vbYesNo, Err.Description) If lngChk = vbYes Then 'close book. Code will proceed to destroy app Wb.Close False Else 'make workbook visible and leave code Wb.Visible = True Exit Sub End If Else MsgBox "The code encountered an error - the file was already closed at this point" & vbNewLine & "Error number " & Err.Number & vbNewLine, Err.Description End If 'destroy app (either if the workbook was closed, or user chose to close it) xlApp.Quit Set xlApp = Nothing End Sub