The following example will allow you to refresh the data connections every time a specific sheet is opened or even schedule it to run at regular intervals. The core idea is to use the Workbook.Connections object to access all data connections and refresh them.
Objective of the VBA Code:
- Automatically update all data connections in the workbook.
- Ensure all connections are refreshed before working with the data.
- Use a workbook open event or a button to trigger the refresh.
VBA Code to Refresh Data Connections
- Refresh Connections when the Workbook is Opened (Using Workbook_Open Event)
If you want to update data connections automatically every time the workbook is opened, you should add the following code inside the ThisWorkbook module.
Steps:
- Open the VBA editor by pressing Alt + F11.
- In the « VBAProject » panel, find « ThisWorkbook » and double-click it to open the code window.
- Copy and paste the following code into the code window.
Private Sub Workbook_Open() ' Call the function to refresh all data connections UpdateAllConnections End Sub Sub UpdateAllConnections() Dim conn As Object ' Loop through all data connections in the workbook For Each conn In ThisWorkbook.Connections ' Try to refresh each connection On Error Resume Next ' Ignore error if connection fails conn.Refresh On Error GoTo 0 ' Reset error handling Next conn MsgBox "All connections have been updated!", vbInformation End Sub
Code Explanation:
- Workbook_Open: This is an event that is automatically executed when the workbook is opened. It calls the UpdateAllConnections function that refreshes all connections.
- UpdateAllConnections: This function loops through all data connections in the workbook (ThisWorkbook.Connections) and attempts to refresh them using conn.Refresh.
- On Error Resume Next: This line ensures that any errors (like if a connection cannot be refreshed) are ignored.
- MsgBox: A message box is displayed once all connections have been refreshed.
- Refresh Connections Using a Button
If you prefer to have a button on a worksheet to trigger the refresh of connections, follow these steps:
Steps:
- In your Excel sheet, insert a form button (via the Developer tab > Insert > Button).
- Link the button to a macro that will refresh the connections.
- Create a standard module to add the following code.
VBA Code:
Sub RefreshConnections() Dim conn As Object ' Loop through to refresh all connections For Each conn In ThisWorkbook.Connections ' Try to refresh each connection On Error Resume Next conn.Refresh On Error GoTo 0 Next conn MsgBox "All connections have been updated!", vbInformation End Sub
Code Explanation:
- This code is very similar to the one used in the Workbook_Open event, except it is triggered manually through a button.
- The RefreshConnections procedure is linked to the button you inserted in the worksheet. When the user clicks the button, all connections are refreshed.
- Automatically Refresh Connections at Regular Intervals (Optional)
If you want the connections to refresh automatically at regular intervals, you can use the following code, which should be placed in a standard module.
VBA Code for Regular Interval Refresh:
Dim NextRefresh As Date
Sub StartAutoRefresh()
' Start automatic refresh every X minutes
NextRefresh = Now + TimeValue("00:05:00") ' Refresh every 5 minutes
Application.OnTime NextRefresh, "AutoRefresh"
End Sub
Sub AutoRefresh()
' Refresh all connections
Call UpdateAllConnections
' Restart automatic refresh
Call StartAutoRefresh
End Sub
Sub UpdateAllConnections()
Dim conn As Object
' Loop to refresh all connections
For Each conn In ThisWorkbook.Connections
On Error Resume Next
conn.Refresh
On Error GoTo 0
Next conn
MsgBox "All connections have been updated!", vbInformation
End Sub
Code Explanation:
- StartAutoRefresh: This macro starts the auto-refresh process by setting the next refresh time using Application.OnTime.
- AutoRefresh: This macro is automatically called at regular intervals to refresh all connections.
- NextRefresh: This variable defines the time for the next refresh (here, it’s set to 5 minutes after the first execution).
Conclusion:
You now have a complete VBA code for automatically updating data connections in an Excel workbook. You can run it on workbook open, via a button, or on a scheduled timer at regular intervals.