Excel VBA: How to Refresh All Data Connections (4 Examples)

In this article, you’ll learn 4 suitable examples of using Excel VBA to refresh all data connections.
The examples included refreshing the dataset when opening a workbook, activating a worksheet and after every fixed interval, or refreshing a Pivot Table using VBA.
Before that, you’ll learn a quick method to create data connections between Excel files from the Data tab.
You can use these methods to automate the process of refreshing data connections in your Excel workbook. This is particularly useful when you have multiple connections that need to be refreshed regularly, such as daily or weekly updates regarding sales or stocked data.
Below you’ll see an overview image showing the use of Excel VBA to refresh all data connections.

Excel VBA refresh all data connections


How to Create Data Connections between Excel Files

Let’s have a quick review of how to create data connections between Excel files. We’ll show here one method using the Data tab. If you want to know about other methods, follow this article: How to Create Excel Data Connection to Another Excel File.

  • We have our original dataset in the sheet named Dataset of Excel file named Source Sheet.
  • We’ll upload this dataset to a new Excel file named Refreshed Sheet.

Showing dataset in original workbook named Source Sheet

  • Open the Workbook Refreshed Sheet and 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 from the folder on your computer.
  • We choose Source Sheet from our New Volume (F:) folder and click on Import.

Choosing Source Sheet workbook from computer’s folder to import

  • The Navigator window will appear.
  • Select the sheet name from where you want to upload your dataset. Our sheet name is Dataset.
  • Then, select the icon beside the Load menu and click on Load To.

Loading dataset from original workbook to Refreshed Sheet workbook

  • The Import Data window will appear.
  • Choose Table as to how you want to view your data.
  • Give the position of the dataset by choosing the Existing worksheet: option and giving the position as cell B2.
  • Press OK to finish.

Choosing different options to import data to Refreshed Sheet workbook

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

Showing imported data in Refreshed Sheet workbook

  • You can format the dataset to make it visually clear.

Showing imported data in Refreshed Sheet workbook after formatting


Refresh All Data Connections Using Excel VBA: 4 Suitable Examples

Now, we’ll be concerned about our original topic of refreshing all data connections using Excel VBA. We’ll discuss 4 examples related to this topic below.


Example 1: Refreshing When Opening Workbook

You can refresh your data connections of the Excel file as soon as you open the workbook automatically. For this, we’ll put a simple VBA code and assign it to trigger an event called Workbook Open in VBA.

  • Open the workbook Refreshed Sheet and go to Developer > Visual Basic.
  • Or use the keyboard shortcut Alt + F11 after opening the workbook Refreshed Sheet to directly open the Visual Basic window.

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

  • Double-click on the ThisWorkbook object.
  • Choose Workbook and Open options from the Module and write the following code there.
Private Sub Workbook_Open()
ThisWorkbook.RefreshAll
MsgBox "Total Sales Data has been Refreshed"
End Sub
  • Save the code by pressing the Save button or Ctrl + S shortcut. You don’t have to run the code.
  • Close the workbook Refreshed Sheet.

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

  • Open the original workbook Source Sheet and change the first 2 Salesman’s names.
  • Save and close the original workbook.

Changing 2 values in original workbook Source Sheet

  • As soon as you open the workbook Refreshed Sheet, you’ll see a message like this.
  • Press OK to continue.

Showing a message showing sales data has been refreshed

  • The dataset is refreshed automatically.

Showing refreshed dataset in Refreshed Sheet workbook

 


Example 2: Refreshing When Activating Worksheet

We want to activate the sheet called Activating Worksheet in the workbook Refreshed Sheet and see the refreshed dataset. So, we’ll write a simple code and assign it to trigger the VBA event Worksheet Activate.

  • Select Visual Basic under the Developer tab from the sheet called Activating Worksheet in the workbook Refreshed Sheet.
  • Alternatively, you can open the Visual Basic window instantly by using the keyboard shortcut Alt + F11.

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

  • Double-click on the Sheet2 (Activating Worksheet) object.
  • Choose Worksheet and Activate options from the Module and write the following code there.
Private Sub Worksheet_Activate()
ThisWorkbook.RefreshAll
MsgBox "Total Sales Data has been Refreshed"
End Sub
  • By using the shortcut Ctrl + S or the Save button, you can save the code. The code need not be executed.
  • Close the workbook Refreshed Sheet.

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

  • Change the first 2 Salesman’s names on the Source Sheet workbook. After that, save the workbook and close it.

Changing 2 values in original workbook Source Sheet

  • As soon as you open the workbook Refreshed Sheet and activate the sheet titled Activating Worksheet, you’ll see a message like this.
  • Press OK to continue.

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 Every Fixed Interval

Suppose you want to refresh your dataset after every fixed interval say 60 seconds or 1 minute. You can easily do this by putting a VBA code.

  • Select Visual Basic under the Developer tab from the workbook Refreshed Sheet.

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

  • Select Module from the Insert menu in the Visual Basic window.

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

  • Enter the following code in the Module and click the Run button or press F5 from the keyboard 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 macro from there and press Run.

Selecting Reload macro to run from Macros window

  • You’ll get the following message. Press OK to continue.

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

  • On the Source Sheet workbook, change the first two Salesmen’s names. The workbook should then be saved and closed.

Changing 2 values in original workbook Source Sheet

  • Wait for 60 seconds and open the Refreshed Sheet workbook. You’ll find the refreshed data.
  • It’ll be refreshed automatically 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

We can also refresh a Pivot Table by running a VBA code.

  • We have a Pivot Table created with our dataset as you can see below.

Showing dataset and Pivot Table

  • After changing the first two Salesmen’s names, the data of the Pivot Table has not changed.
  • So, we have to refresh it.

Changing 2 values in dataset but Pivot Table is not updating

  • Select Visual Basic under the Developer tab to open the Visual Basic window.

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

  • From the Insert menu in the Visual Basic window, choose Module.

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

  • To run the code, type the following into the Module and either click Run or hit F5 on your keyboard.
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

  • After running the code, you’ll see that the Pivot Table has been refreshed.

Showing refreshed Pivot Table after running VBA code

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


Things to Remember

  • We are working with 2 workbooks here named Source Sheet and Refreshed Sheet. The Source Sheet workbook is saved in xlsx format and the Refreshed Sheet workbook is saved in xlsm format.
  • The shortcut key for opening the Visual Basic window is Alt + F11.
  • The shortcut key for running the VBA code is F5.

Frequently Asked Questions

1. How do I refresh all data connections manually in Excel?
Go to the Data tab and in the Connections group, click on the Refresh All button. Once the refresh is complete, the data from all the connections will be updated. You can also use the keyboard shortcut Ctrl+Alt+F5 to refresh your data connections manually.

2. How do I refresh multiple connections in Excel?
Go to the Data tab and in the Connections group, click on the Connections button. This will open the Workbook Connections window. In the window, select the connections you want to refresh. You can use Ctrl+Click or Shift+Click to select multiple connections. After selecting the desired connections, click on the Refresh button. Excel will refresh the selected connections, and the updated data will be shown in your workbook.


Download Practice Workbook

You can download the Excel files from here for practice.


Conclusion

In conclusion, you have learned to use Excel VBA to automate the process of refreshing data connections in your workbook through this article. The article has shown different examples regarding refreshing data when opening a workbook, activating a worksheet, or at fixed intervals. Additionally, the ability to refresh Pivot Tables using VBA enhances your data analysis power. Please let us know in the comment section if there is any query or suggestions related to this topic.


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