[英]Refresh power query VBA
So I use the below code to refresh my Query connections, however, how can I display a message if my refresh failed due to wtv reason?所以我使用下面的代码来刷新我的查询连接,但是,如果由于 wtv 原因导致刷新失败,我该如何显示消息? as this VBA shows me refresh complete even though there are multiple errors with my queries.
因为这个 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
Belive it will stop at Connection.Refresh
- with the Debugger.相信它会停止在
Connection.Refresh
- 使用调试器。 And if you want to catch this Error in your Sub you need to define something like On Error Goto MyError
and then handle the Error with a message or something.如果你想在你的 Sub 中捕获这个错误,你需要定义类似
On Error Goto MyError
的东西,然后用消息或其他东西处理错误。 Resume Next
will then continue your code on the next line of your error. Resume Next
将在错误的下一行继续您的代码。
Exit Sub
MyError:
MsgBox (Err.Description)
resume next
End Sub
Described here at: Microsoft Learn此处描述于: Microsoft Learn
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.