Although Excel does a great job at saving data in a nice table structure, some projects require data to be stored or exported elsewhere. Text files, other excel files, databases, or even web pages can be used as a means to consume or store data. If you need to periodically update your data, moving or copying to an Excel spreadsheet can be time consuming and prone to errors. Data connection features of Excel can help establish permanent links with other data sources and automate this process.
Excel can pull data from an external data source into your spreadsheet with the help of data connection features. Excel can connect external data sources when you provide certain information about the external data and allows you to refresh them manually, automatically in specified intervals, or in a more customized fashion using VBA. In this article, we're going to be using an equipment rental data and connect it with data from a Microsoft Access database. You can download the workbook and the Access file below.
Data connection features can be found under the DATA tab and consists of two categories:
- Get External Data
Features under the Get External Data section help create a connection with sources like other workbooks, databases, text files, or websites. Connections section contains features for managing existing connections. To begin creating a new link, click Connections. You will be taken to the Workbook Connections window.
When you connect an external data in Excel, data will be first saved in the workbook. This data can then be exported in other formats, such as Office Data Connection (ODC) (.odc), or a Universal Data Connection (UDC) file (.udcx), to save or share the information.
Creating a data connection
Follow the steps below to connect an external data source to your workbook.
- Begin by going to the Data tab and selecting the connection type that corresponds to your data source. In this example, we are using a Microsoft Access database. Please note that the exact steps will be different if you choose another data type. For example, while you need to select a file for an Access or a text file, you will need to enter the server address for a SQL database. Click the From Access icon to connect to an Access file.
- Select your file and click Open.
- Select Table window will appear. Select the table you’d like to use and click OK to continue.
- In the Import Data window, you can choose how and where to display your data, and you can access advanced settings by pressing the Properties You don’t have to this now as you can open the Properties window at any time after creating connection.
In our example, we choose the Table option. Here is brief summary of all options.
- Table: Data is displayed in a tabular form
- PivotTable Report: You can use the columns to create a PivotTable
- PivotChart: You can use the columns to create a PivotChart
- Only Create Connection: Data will not be displayed, but the connection can be used by other features or VBA, and can be exported.
- Add this data to Data Model: Data is added to the Power Pivot Data Model. This option is independent from the display option above. However, it is advisable to use it with Only Create Connection option, because you can manage your data better in the Data Model.
You can choose to pull certain columns, instead of the entire table, if necessary. Try the PivotTable and Only Create Connection options to create connections without displaying the data in your spreadsheet.
Refreshing and managing data connections
There are several ways to refresh existing connections. The first method is by using the icons under both DATA and TABLE TOOLS - DESIGN tabs in the ribbon. These icons can be used to refresh data connection manually.
Both options essentially do the same thing. However, the default actions are different for the Refresh buttons in the DATA and TABLE TOOLS – DESIGN tabs. While Refresh All action is the default action in DATA tab. Refresh action is the default in the TABLE TOOLS – DESIGN tab. The Refresh button only refreshes the active table's connection.
Power Pivot and Power Query windows also contain refresh buttons.
To automate this process, you can set a time interval to refresh data connections. To set a refresh interval, and manage the connections (including advanced Properties), use the Connections button to open the Workbook Connections window. You can add new connections, or refresh existing ones in this menu. Select the existing connection and click the Properties button to open Connection Properties window.
Enable the Refresh every option and modify the number that represents minutes. When you click OK, your data connection will be updated in selected intervals.
An alternative way to automatically refresh data is using VBA. This allows combining data connection features with your own custom code. Below is sample codes that can refresh data connections.
ActiveWorkbook.Connections("test").Refresh 'Refresh only the connection named "test" ActiveWorkbook.RefreshAll 'Refresh all connections