Excel VBA: How to Refresh All Data Connections – 4 Examples

This is an overview.

Excel VBA refresh all data connections


Create Data Connections between Excel Files

The original dataset is the Dataset sheet in the Source Sheet workbook .

  • Upload this dataset to a new book: Refreshed Sheet.

Showing dataset in original workbook named Source Sheet

  • In the Refreshed Sheet, go to Data > Get Data > From File > From Excel Workbook.

Uploading dataset from original workbook by using Get Data option under Data tab

  • Choose the file you want to upload.
  • Here,  Source Sheet from New Volume (F:).
  • Click Import.

Choosing Source Sheet workbook from computer’s folder to import

  • In the Navigator window select the sheet containing the dataset you want to upload. Dataset, here.
  • Go to Load and click Load To.

Loading dataset from original workbook to Refreshed Sheet workbook

  • In the Import Data window select Table.
  • Choose Existing worksheet: to provide the position of the dataset and enter B2.
  • Click OK.

Choosing different options to import data to Refreshed Sheet workbook

  • Your data will be uploaded to the Refreshed Sheet in table format. It’ll be connected with the Source Sheet.

Showing imported data in Refreshed Sheet workbook

  • You can format the dataset:

Showing imported data in Refreshed Sheet workbook after formatting


How to Refresh All Data Connections Using Excel VBA – 4 Examples

Example 1 – Refreshing When Opening Workbook

  • Open the workbook Refreshed Sheet and go to Developer > Visual Basic. You can also use Alt + F11 to open Visual Basic.

Opening Visual Basic window from Developer tab to use Excel VBA to refresh all data connections

  • Double-click  ThisWorkbook.
  • Choose WorkbookOpenModule and enter the following code.
Private Sub Workbook_Open()
ThisWorkbook.RefreshAll
MsgBox "Total Sales Data has been Refreshed"
End Sub
  • Click Save or press Ctrl + S. You don’t have to run the code.
  • Close the Refreshed Sheet.

Inserting code in Module and triggering Workbook Open event to use Excel VBA to refresh all data connections

  • Open the Source Sheet and change the two first names in Salesman.
  • Save and close.

Changing 2 values in original workbook Source Sheet

  • Open the Refreshed Sheet. This message will be displayed.
  • Click OK.

Showing a message showing sales data has been refreshed

The dataset is automatically refreshed .

Showing refreshed dataset in Refreshed Sheet workbook

 


Example 2 – Refreshing When Activating the Worksheet

  • Select Visual Basic in the Developer tab in the Activating Worksheet (Refreshed Sheet workbook). You can also use Alt + F11 to open the Visual Basic.

Opening Visual Basic window from Developer tab to use Excel VBA to refresh all data connections

  • Double-click  Sheet2 (Activating Worksheet).
  • Choose Module> Worksheet >Activate and enter the following code.
Private Sub Worksheet_Activate()
ThisWorkbook.RefreshAll
MsgBox "Total Sales Data has been Refreshed"
End Sub
  • Click Save or press Ctrl + S. You don’t have to run the code.
  • Close the Refreshed Sheet.

Inserting code in Module and triggering Worksheet Activate event to use Excel VBA to refresh all data connections

  • Change the two first names in Salesman in the Source Sheet.
  • Save and close.

Changing 2 values in original workbook Source Sheet

  • Open the Refreshed Sheet and activate the Activating Worksheet. This message will be displayed.
  • Click OK.

Showing a message showing sales data has been refreshed

The dataset is automatically updated.

Showing refreshed dataset in Refreshed Sheet workbook


Example 3 – Refreshing After a Fixed Interval with VBA

60 seconds or 1 minute is the fixed interval, here.

  • In the Refreshed Sheet select Visual Basic in the Developer tab.

Opening Visual Basic window from Developer tab to use Excel VBA to refresh all data connections

  • In Visual Basic choose Insert and select Module.

Selecting Module from Insert tab in Visual Basic window to use Excel VBA to refresh all data connections

  • Enter the following code and click Run or press F5 to run the code.
Public ReloadInterval As Double
Public Const Period = 60
Sub Reload()
MsgBox "Total Sales Data will be Refreshed at " & _
"the interval of " & Period & " seconds"
Call FirstReload
End Sub
Sub FirstReload()
ReloadInterval = Now + TimeSerial(0, 0, Period)
Application.OnTime _
EarliestTime:=ReloadInterval, _
Procedure:="ReloadConnections", _
Schedule:=True
End Sub
Sub ReloadConnections()
ThisWorkbook.RefreshAll
Call FirstReload
End Sub

Inserting code in Module and pressing Run button to use Excel VBA to refresh all data connections

  • The Macros window will open.
  • Select Reload and click Run.

Selecting Reload macro to run from Macros window

  • This message will be displayed. Click OK.

Showing a message showing sales data will be refreshed after every 60 seconds

  • In the Source Sheet, change the two first names in Salesman. Save and close.

Changing 2 values in original workbook Source Sheet

  • Wait 60 seconds and open the Refreshed Sheet.

Data is automatically refreshed after every 60 seconds or 1 minute.

Showing refreshed dataset in Refreshed Sheet workbook

Read More: How to Refresh Data Connection in Excel Without Opening File


Example 4 – Refreshing a Pivot Table

  • This is the Pivot Table.

Showing dataset and Pivot Table

  • Changing the two first names in Salesman won’t change data in the Pivot Table. It must be refreshed.

Changing 2 values in dataset but Pivot Table is not updating

  • Select Visual Basic in the Developer tab.

Opening Visual Basic window from Developer tab to use Excel VBA to refresh all data connections

  • In Visual Basic choose Insert and select Module.

Selecting Module from Insert tab in Visual Basic window to use Excel VBA to refresh all data connections

  • Enter the following code and click Run or press F5 to run the code.
Sub Refreshing_Pivot_Table()
ActiveSheet.PivotTables("PivotTable1").RefreshTable
End Sub

Inserting code in Module and pressing Run button to use Excel VBA to refresh all data connections

The Pivot Table is refreshed.

Showing refreshed Pivot Table after running VBA code

Read More: [Fixed]! Data Connection Not Refreshing in Excel


Download Practice Workbook

Download the Excel files and practice.


Related Arrticles

<< Go Back to Excel Data Connections | Importing Data in Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Sajid Ahmed
Sajid Ahmed

Sajid Ahmed, a BSc graduate in Naval Architecture & Engineering from Bangladesh University of Engineering and Technology, assumes the position of an Excel & VBA Content Developer at ExcelDemy. A self-motivated individual, his profound interest in research and innovation aligns seamlessly with his passion for Excel. In this role, Sajid not only adeptly addresses challenges but also demonstrates enthusiasm and expertise in gracefully navigating complex situations. This underscores his steadfast commitment to consistently delivering exceptional content. His interests... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo