【发布时间】:2018-06-02 23:36:00
【问题描述】:
我是一名初学者 python 程序员,但最近我在 access 和 excel 中使用 VBA 进行了一些编码。
我正在编写从两个不同查询(从访问 Excel 工作簿)导出一些数据的代码。然后,我继续从 excel 中对数据进行更多处理,以便进行一些销售分析。
问题是,有时,查询之一(或两者)可能为空,这会引发
错误“3021”
我想做的是,如果一个
错误“3021”
发生在特定的块中,跳转到特定的行并继续执行。
我会在 python 中放置那个代码块——我知道它可以提高
错误“3021”
在“尝试”内避免执行
#code here
Try:
#code that may raise error
Except Exception as '3021':
pass
#code here....
在 VBA 中我能够处理
错误“3021”
但不是我想要的。一旦我“管理”了
错误“3021”
如果在其余代码中引发另一个错误,则最后一个错误处理程序始终处于活动状态(隐藏我想引发以进行调试的其他错误引发。代码摘录如下:
On Error GoTo Err1Handler
'here starts the code that may raise the error '3021'
consulta16.MoveFirst
Datos16.Range("A2").CopyFromRecordset consulta16
With APIExcel.ActiveSheet.Cells
.Select
.EntireColumn.AutoFit
.Range("A1").Select
End With
'From here I know there wont be an error '3021'
NoData16:
columnas = consulta17.Fields.Count
For i = 0 To columnas - 1
Datos17.Cells(1, i + 1) = consulta17.Fields(i).Name
Next i
'Now comes the second query that may raise same
错误“3021”
所以我又犯了一个错误异常
On Error GoTo Err2Handler
consulta17.MoveFirst
Datos17.Range("A2").CopyFromRecordset consulta17
With APIExcel.ActiveSheet.Cells
.Select
.EntireColumn.AutoFit
.Range("A1").Select
End With
'#more code.......
Err1Handler:
If Err.Number = 3021 Then
'MsgBox ("Sin datos del 16")
Resume NoData16
End If
Err2Handler:
If Err.Number = 3021 Then
'MsgBox ("Sin datos del 17")
Resume NoData17
End If
我读过here This documentation about error handling in Vba 但我不知道如何在不影响整个脚本的情况下处理特定错误。我想我正试图将 vba 错误处理作为 python 中的编码来处理......如果有人能帮助我理解如何在不影响整个脚本的情况下隔离错误处理程序中的代码块,我将不胜感激(尝试/捕获/最终方式? )
【问题讨论】:
-
如果这个问题与python无关,你应该删除python代码和标签并专注于你遇到的vba问题。
标签: vba excel error-handling