刷新电源查询VBA



所以我使用下面的代码来刷新我的查询连接,但是,如果由于wtv原因刷新失败,我如何显示消息?因为这个VBA显示我刷新完成,即使有多个错误与我的查询。

'Worksheets("Details").Unprotect
Dim Connection As WorkbookConnection
Dim bugfix As Integer

For bugfix = 1 To 2
On Error Resume Next

For Each Connection In ActiveWorkbook.Connections

With Connection

If (.Type = xlConnectionTypeODBC) Then
.ODBCConnection.BackgroundQuery = False

Else

If (.Type = xlConnectionTypeOLEDB) Then
.OLEDBConnection.BackgroundQuery = False

End If

End If

End With

Connection.Refresh

Next Connection

Next bugfix
'Worksheets("Details").Protect , AllowFiltering:=True, AllowFormattingCells:=True, DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFormattingColumns:=True
MsgBox "Refresh Complete"
End Sub

相信它将停止在Connection.Refresh-与调试器。如果你想在Sub中捕获这个错误,你需要定义像On Error Goto MyError这样的东西,然后用消息或其他东西来处理这个错误。然后Resume Next将在错误的下一行继续您的代码。

Exit Sub
MyError:
MsgBox (Err.Description)
resume next
End Sub

在这里描述:Microsoft Learn

最新更新