79564679

Date: 2025-04-09 15:04:48
Score: 1.5
Natty:
Report link

If you want a specific error handling, then this is what I recommend:

Sub RefreshConnectionsWithErrorHandler()
    Dim cn As WorkbookConnection
    Dim isPowerQueryConnection As Boolean
    Dim errMsg As String

    On Error GoTo ErrorHandler  ' Enable error handling for the entire sub

    ' Loop through all connections
    For Each cn In ActiveWorkbook.Connections
        isPowerQueryConnection = InStr(1, cn.OLEDBConnection.Connection, "Provider=Microsoft.Mashup.OleDb.1") > 0
        
        ' Refresh each connection
        If isPowerQueryConnection Then
            cn.OLEDBConnection.BackgroundQuery = False ' Disable background refresh for better error visibility
            
            On Error Resume Next ' temporarily ignore errors within the single connection refresh
            cn.Refresh
             On Error GoTo ErrorHandler ' re-enable normal error handling
            
            ' Check for an error condition by checking if the connection was successfully refreshed.
             If Err.Number <> 0 Then
                errMsg = "Error refreshing connection '" & cn.Name & "': " & Err.Description
                Debug.Print errMsg
            Else
                Debug.Print "Connection '" & cn.Name & "' refreshed successfully."
            End If

            Err.Clear ' Clear the error, so you do not get the same error for multiple connections.
        Else
             Debug.Print "Skipping non Power Query connection: " & cn.Name
        End If
    Next cn

    MsgBox "All connections processed. Check the Immediate Window for details.", vbInformation
    Exit Sub

ErrorHandler:
    ' Handle general errors
    MsgBox "An unexpected error occurred during refresh. Error: " & Err.Description, vbCritical
End Sub

Error Checking and Handling:

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): get the same error
  • Low reputation (0.5):
Posted by: user80346