Abstract: This is the first tutorial in a series designed to get you acquainted and comfortable using Excel and its built-in data mash-up and analysis features. These tutorials build and refine an Excel workbook from scratch, build a data model, then create amazing interactive reports using Power View. The tutorials are designed to demonstrate Microsoft Business Intelligence features and capabilities in Excel, PivotTables, Power Pivot, and Power View.
Note: This article describes data models in Excel 2013. However, the same data modeling and Power Pivot features introduced in Excel 2013 also apply to Excel 2016.
In these tutorials you learn how to import and explore data in Excel, build and refine a data model using Power Pivot, and create interactive reports with Power View that you can publish, protect, and share.
The tutorials in this series are the following:
-
Import Data into Excel 2013, and Create a Data Model
-
Extend Data Model relationships using Excel, Power Pivot, and DAX
-
Create Map-based Power View Reports
-
Incorporate Internet Data, and Set Power View Report Defaults
-
Power Pivot Help
-
Create Amazing Power View Reports — Part 2
In this tutorial, you start with a blank Excel workbook.
The sections in this tutorial are the following:
-
Import data from a database
-
Import data from a spreadsheet
-
Import data using copy and paste
-
Create a relationship between imported data
-
Checkpoint and Quiz
At the end of this tutorial is a quiz you can take to test your learning.
This tutorial series uses data describing Olympic Medals, hosting countries, and various Olympic sporting events. We suggest you go through each tutorial in order. Also, tutorials use Excel 2013 with Power Pivot enabled. For more information on Excel 2013, click here. For guidance on enabling Power Pivot, click here.
Import data from a database
We start this tutorial with a blank workbook. The goal in this section is to connect to an external data source, and import that data into Excel for further analysis.
Let’s start by downloading some data from the Internet. The data describes Olympic Medals, and is a Microsoft Access database.
-
Click the following links to download files we use during this tutorial series. Download each of the four files to a location that’s easily accessible, such as Downloads or My Documents, or to a new folder you create:
> OlympicMedals.accdb Access database
> OlympicSports.xlsx Excel workbook
> Population.xlsx Excel workbook
> DiscImage_table.xlsx Excel workbook -
In Excel 2013, open a blank workbook.
-
Click DATA > Get External Data > From Access. The ribbon adjusts dynamically based on the width of your workbook, so the commands on your ribbon may look slightly different from the following screens. The first screen shows the ribbon when a workbook is wide, the second image shows a workbook that has been resized to take up only a portion of the screen.
-
Select the OlympicMedals.accdb file you downloaded and click Open. The following Select Table window appears, displaying the tables found in the database. Tables in a database are similar to worksheets or tables in Excel. Check the Enable selection of multiple tables box, and select all the tables. Then click OK.
-
The Import Data window appears.
Note: Notice the checkbox at the bottom of the window that allows you to Add this data to the Data Model, shown in the following screen. A Data Model is created automatically when you import or work with two or more tables simultaneously. A Data Model integrates the tables, enabling extensive analysis using PivotTables, Power Pivot, and Power View. When you import tables from a database, the existing database relationships between those tables is used to create the Data Model in Excel. The Data Model is transparent in Excel, but you can view and modify it directly using the Power Pivot add-in. The Data Model is discussed in more detail later in this tutorial.
Select the PivotTable Report option, which imports the tables into Excel and prepares a PivotTable for analyzing the imported tables, and click OK.
-
Once the data is imported, a PivotTable is created using the imported tables.
With the data imported into Excel, and the Data Model automatically created, you’re ready to explore the data.
Explore data using a PivotTable
Exploring imported data is easy using a PivotTable. In a PivotTable, you drag fields (similar to columns in Excel) from tables (like the tables you just imported from the Access database) into different areas of the PivotTable to adjust how it presents your data. A PivotTable has four areas: FILTERS, COLUMNS, ROWS, and VALUES.
It might take some experimenting to determine which area a field should be dragged to. You can drag as many or few fields from your tables as you like, until the PivotTable presents your data how you want to see it. Feel free to explore by dragging fields into different areas of the PivotTable; the underlying data is not affected when you arrange fields in a PivotTable.
Let’s explore the Olympic Medals data in the PivotTable, starting with Olympic medalists organized by discipline, medal type, and the athlete’s country or region.
-
In PivotTable Fields, expand the Medals table by clicking the arrow beside it. Find the NOC_CountryRegion field in the expanded Medals table, and drag it to the COLUMNS area. NOC stands for National Olympic Committees, which is the organizational unit for a country or region.
-
Next, from the Disciplines table, drag Discipline to the ROWS area.
-
Let’s filter Disciplines to display only five sports: Archery, Diving, Fencing, Figure Skating, and Speed Skating. You can do this from within the PivotTable Fields area, or from the Row Labels filter in the PivotTable itself.
-
Click anywhere in the PivotTable to ensure the Excel PivotTable is selected. In the PivotTable Fields list, where the Disciplines table is expanded, hover over its Discipline field and a dropdown arrow appears to the right of the field. Click the dropdown, click (Select All)to remove all selections, then scroll down and select Archery, Diving, Fencing, Figure Skating, and Speed Skating. Click OK.
-
Or, in the Row Labels section of the PivotTable, click the dropdown next to Row Labels in the PivotTable, click (Select All) to remove all selections, then scroll down and select Archery, Diving, Fencing, Figure Skating, and Speed Skating. Click OK.
-
-
In PivotTable Fields, from the Medals table, drag Medal to the VALUES area. Since Values must be numeric, Excel automatically changes Medal to Count of Medal.
-
From the Medals table, select Medal again and drag it into the FILTERS area.
-
Let’s filter the PivotTable to display only those countries or regions with more than 90 total medals. Here’s how.
-
In the PivotTable, click the dropdown to the right of Column Labels.
-
Select Value Filters and select Greater Than….
-
Type 90 in the last field (on the right). Click OK.
-
Your PivotTable looks like the following screen.
With little effort, you now have a basic PivotTable that includes fields from three different tables. What made this task so simple were the pre-existing relationships among the tables. Because table relationships existed in the source database, and because you imported all the tables in a single operation, Excel could recreate those table relationships in its Data Model.
But what if your data originates from different sources, or is imported at a later time? Typically, you can create relationships with new data based on matching columns. In the next step, you import additional tables, and learn how to create new relationships.
Import data from a spreadsheet
Now let’s import data from another source, this time from an existing workbook, then specify the relationships between our existing data and the new data. Relationships let you analyze collections of data in Excel, and create interesting and immersive visualizations from the data you import.
Let’s start by creating a blank worksheet, then import data from an Excel workbook.
-
Insert a new Excel worksheet, and name it Sports.
-
Browse to the folder that contains the downloaded sample data files, and open OlympicSports.xlsx.
-
Select and copy the data in Sheet1. If you select a cell with data, such as cell A1, you can press Ctrl + A to select all adjacent data. Close the OlympicSports.xlsx workbook.
-
On the Sports worksheet, place your cursor in cell A1 and paste the data.
-
With the data still highlighted, press Ctrl + T to format the data as a table. You can also format the data as a table from the ribbon by selecting HOME > Format as Table. Since the data has headers, select My table has headers in the Create Table window that appears, as shown here.
Formatting the data as a table has many advantages. You can assign a name to a table, which makes it easy to identify. You can also establish relationships between tables, enabling exploration and analysis in PivotTables, Power Pivot, and Power View.
-
Name the table. In TABLE TOOLS > DESIGN > Properties, locate the Table Name field and type Sports. The workbook looks like the following screen.
-
Save the workbook.
Import data using copy and paste
Now that we’ve imported data from an Excel workbook, let’s import data from a table we find on a web page, or any other source from which we can copy and paste into Excel. In the following steps, you add the Olympic host cities from a table.
-
Insert a new Excel worksheet, and name it Hosts.
-
Select and copy the following table, including the table headers.
City |
NOC_CountryRegion |
Alpha-2 Code |
Edition |
Season |
Melbourne / Stockholm |
AUS |
AS |
1956 |
Summer |
Sydney |
AUS |
AS |
2000 |
Summer |
Innsbruck |
AUT |
AT |
1964 |
Winter |
Innsbruck |
AUT |
AT |
1976 |
Winter |
Antwerp |
BEL |
BE |
1920 |
Summer |
Antwerp |
BEL |
BE |
1920 |
Winter |
Montreal |
CAN |
CA |
1976 |
Summer |
Lake Placid |
CAN |
CA |
1980 |
Winter |
Calgary |
CAN |
CA |
1988 |
Winter |
St. Moritz |
SUI |
SZ |
1928 |
Winter |
St. Moritz |
SUI |
SZ |
1948 |
Winter |
Beijing |
CHN |
CH |
2008 |
Summer |
Berlin |
GER |
GM |
1936 |
Summer |
Garmisch-Partenkirchen |
GER |
GM |
1936 |
Winter |
Barcelona |
ESP |
SP |
1992 |
Summer |
Helsinki |
FIN |
FI |
1952 |
Summer |
Paris |
FRA |
FR |
1900 |
Summer |
Paris |
FRA |
FR |
1924 |
Summer |
Chamonix |
FRA |
FR |
1924 |
Winter |
Grenoble |
FRA |
FR |
1968 |
Winter |
Albertville |
FRA |
FR |
1992 |
Winter |
London |
GBR |
UK |
1908 |
Summer |
London |
GBR |
UK |
1908 |
Winter |
London |
GBR |
UK |
1948 |
Summer |
Munich |
GER |
DE |
1972 |
Summer |
Athens |
GRC |
GR |
2004 |
Summer |
Cortina d’Ampezzo |
ITA |
IT |
1956 |
Winter |
Rome |
ITA |
IT |
1960 |
Summer |
Turin |
ITA |
IT |
2006 |
Winter |
Tokyo |
JPN |
JA |
1964 |
Summer |
Sapporo |
JPN |
JA |
1972 |
Winter |
Nagano |
JPN |
JA |
1998 |
Winter |
Seoul |
KOR |
KS |
1988 |
Summer |
Mexico |
MEX |
MX |
1968 |
Summer |
Amsterdam |
NED |
NL |
1928 |
Summer |
Oslo |
NOR |
NO |
1952 |
Winter |
Lillehammer |
NOR |
NO |
1994 |
Winter |
Stockholm |
SWE |
SW |
1912 |
Summer |
St Louis |
USA |
US |
1904 |
Summer |
Los Angeles |
USA |
US |
1932 |
Summer |
Lake Placid |
USA |
US |
1932 |
Winter |
Squaw Valley |
USA |
US |
1960 |
Winter |
Moscow |
URS |
RU |
1980 |
Summer |
Los Angeles |
USA |
US |
1984 |
Summer |
Atlanta |
USA |
US |
1996 |
Summer |
Salt Lake City |
USA |
US |
2002 |
Winter |
Sarajevo |
YUG |
YU |
1984 |
Winter |
-
In Excel, place your cursor in cell A1 of the Hosts worksheet and paste the data.
-
Format the data as a table. As described earlier in this tutorial, you press Ctrl + T to format the data as a table, or from HOME > Format as Table. Since the data has headers, select My table has headers in the Create Table window that appears.
-
Name the table. In TABLE TOOLS > DESIGN > Properties locate the Table Name field, and type Hosts.
-
Select the Edition column, and from the HOME tab, format it as Number with 0 decimal places.
-
Save the workbook. Your workbook looks like the following screen.
Now that you have an Excel workbook with tables, you can create relationships between them. Creating relationships between tables lets you mash up the data from the two tables.
Create a relationship between imported data
You can immediately begin using fields in your PivotTable from the imported tables. If Excel can’t determine how to incorporate a field into the PivotTable, a relationship must be established with the existing Data Model. In the following steps, you learn how to create a relationship between data you imported from different sources.
-
On Sheet1, at the top ofPivotTable Fields, clickAll to view the complete list of available tables, as shown in the following screen.
-
Scroll through the list to see the new tables you just added.
-
Expand Sports and select Sport to add it to the PivotTable. Notice that Excel prompts you to create a relationship, as seen in the following screen.
This notification occurs because you used fields from a table that’s not part of the underlying Data Model. One way to add a table to the Data Model is to create a relationship to a table that’s already in the Data Model. To create the relationship, one of the tables must have a column of unique, non-repeated, values. In the sample data, the Disciplines table imported from the database contains a field with sports codes, called SportID. Those same sports codes are present as a field in the Excel data we imported. Let’s create the relationship.
-
Click CREATE… in the highlighted PivotTable Fields area to open the Create Relationship dialog, as shown in the following screen.
-
In Table, choose Disciplines from the drop down list.
-
In Column (Foreign), choose SportID.
-
In Related Table, choose Sports.
-
In Related Column (Primary), choose SportID.
-
Click OK.
The PivotTable changes to reflect the new relationship. But the PivotTable doesn’t look right quite yet, because of the ordering of fields in the ROWS area. Discipline is a subcategory of a given sport, but since we arranged Discipline above Sport in the ROWS area, it’s not organized properly. The following screen shows this unwanted ordering.
-
In the ROWS area, move Sport above Discipline. That’s much better, and the PivotTable displays the data how you want to see it, as shown in the following screen.
Behind the scenes, Excel is building a Data Model that can be used throughout the workbook, in any PivotTable, PivotChart, in Power Pivot, or any Power View report. Table relationships are the basis of a Data Model, and what determine navigation and calculation paths.
In the next tutorial, Extend Data Model relationships using Excel 2013, Power Pivot, and DAX, you build on what you learned here, and step through extending the Data Model using a powerful and visual Excel add-in called Power Pivot. You also learn how to calculate columns in a table, and use that calculated column so that an otherwise unrelated table can be added to your Data Model.
Checkpoint and Quiz
Review What You’ve Learned
You now have an Excel workbook that includes a PivotTable accessing data in multiple tables, several of which you imported separately. You learned to import from a database, from another Excel workbook, and from copying data and pasting it into Excel.
To make the data work together, you had to create a table relationship that Excel used to correlate the rows. You also learned that having columns in one table that correlate to data in another table is essential for creating relationships, and for looking up related rows.
You’re ready for the next tutorial in this series. Here’s a link:
Extend Data Model relationships using Excel 2013, Power Pivot, and DAX
QUIZ
Want to see how well you remember what you learned? Here’s your chance. The following quiz highlights features, capabilities, or requirements you learned about in this tutorial. At the bottom of the page, you’ll find the answers. Good luck!
Question 1: Why is it important to convert imported data into tables?
A: You don’t have to convert them into tables, because all imported data is automatically turned into tables.
B: If you convert imported data into tables, they will be excluded from the Data Model. Only when they’re excluded from the Data Model are they available in PivotTables, Power Pivot, and Power View.
C: If you convert imported data into tables, they can be included in the Data Model, and be made available to PivotTables, Power Pivot, and Power View.
D: You cannot convert imported data into tables.
Question 2: Which of the following data sources can you import into Excel, and include in the Data Model?
A: Access Databases, and many other databases as well.
B: Existing Excel files.
C: Anything you can copy and paste into Excel and format as a table, including data tables in websites, documents, or anything else that can be pasted into Excel.
D: All of the above
Question 3: In a PivotTable, what happens when you reorder fields in the four PivotTable Fields areas?
A: Nothing – you cannot reorder fields once you place them in the PivotTable Fields areas.
B: The PivotTable format is changed to reflect the layout, but underlying data is unaffected.
C: The PivotTable format is changed to reflect the layout, and all underlying data is permanently changed.
D: The underlying data is changed, resulting in new data sets.
Question 4: When creating a relationship between tables, what is required?
A: Neither table can have any column that contains unique, non-repeated values.
B: One table must not be part of the Excel workbook.
C: The columns must not be converted to tables.
D: None of the above is correct.
Quiz Answers
-
Correct answer: C
-
Correct answer: D
-
Correct answer: B
-
Correct answer: D
Notes: Data and images in this tutorial series are based on the following:
-
Olympics Dataset from Guardian News & Media Ltd.
-
Flag images from CIA Factbook (cia.gov)
-
Population data from The World Bank (worldbank.org)
-
Olympic Sport Pictograms by Thadius856 and Parutakupiu
How to Import Data in Excel?
Importing the data from another file or another source file is often required in Excel. For example, sometimes people need data directly from very complicated servers, and sometimes we may need to import data from a text file or even from an Excel workbook.
If you are new to Excel data importing, then in this article, we will take a tour of importing data from text files, different Excel workbooks, and MS Access. Follow this article to learn the process involved in importing the data.
Table of contents
- How to Import Data in Excel?
- #1 – Import Data from Another Excel Workbook
- #2 – Import Data from MS Access to Excel
- #3 – Import Data from Text File to Excel
- Things to Remember
- Recommended Articles
#1 – Import Data from Another Excel Workbook
You can download this Import Data Excel Template here – Import Data Excel Template
Let us start.
- First, we must go to the DATA tab. Then, under the DATA tab, click on Connections.
- As soon as we click on Connections, we may see the below window separately.
- Now, click on Add.
- It will open up a new window. In the below window, select All Connections.
- If there are any connections in this workbook, it will show what those connections are here.
- Since we are connecting to a new workbook, click on Browse for more.
- In the below window, browse the file location. Then, click on Open.
- After clicking on Open, it shows the below window.
- Here, we need to select the required table to be imported to this workbook. Select the table and click on OK.
After clicking on OK, close the Workbook Connection window.
- Then, go to Existing Connections under the DATA tab.
- Here, we will see all the existing connections. Select the connection we have just made and click on Open.
- Once we click on Open, it will ask us where to import the data. First, we need to select the cell reference here. Then, click on the OK button.
- It will import the data from the selected or connected workbook.
Like this, we can connect to the other workbook and import the data.
#2 – Import Data from MS Access to Excel
MS Access is the main platform to store the data safely. We can import the data directly from the MS Access File itself whenever the data is required.
Step 1: Go to the “DATA” ribbon in excelThe ribbon is an element of the UI (User Interface) which is seen as a strip that consists of buttons or tabs; it is available at the top of the excel sheet. This option was first introduced in the Microsoft Excel 2007.read more and select “From Access.“
Step 2: Now, it will ask us to locate the desired file. Select the desired file path. Then, click on “Open.”
Step 3: Now, it will ask us to select the desired destination cell where we want to import the data. Then, click on “OK.”
Step 4: It will import the data from access to the A1 cell in Excel.
#3 – Import Data from Text File to Excel
In almost all the corporations, whenever we ask for the data from the IT team, they will write a query and get the file in TEXT format. But unfortunately, TEXT file data is not the ready format to use in Excel; we need to make some modifications to work on it.
Step 1: Go to the “DATA” tab and click on “From Text.”
Step 2: Now, it will ask us to choose the file location on the computer or laptop. Select the targeted file, then click on “Import.”
Step 3: It will open up a “Text Import Wizard.”
Step 4: By selecting the “Delimited,” click on “Next.”
Step 5: In the next window, select the other and mention comma (,) because, in the text file, each column is separated by a comma (,). Then click on “Next.”
Step 6: In the next window, click on “Finish.”
Step 7: Now, it will ask us to select the desired destination cell where we want to import the data. Select the cell and click on “OK.”
Step 8: It will import the data from the text file to the A1 cell in Excel.
Things to Remember
- If there are many tables, we must specify which table data we need to import.
- If we want the data in the current worksheet, we need to select the desired cell. Else, if we need data in a new worksheet, we must choose the new worksheet as the option.
- We need to separate the column in the TEXT file by identifying the common column separators.
Recommended Articles
This article is a guide to Import Data in Excel. Here, we discuss how to import data from 1) Excel Workbook, 2) MS Access, 3) Text File, practical examples, and a downloadable Excel template. You may learn more about Excel from the following articles: –
- KPI Dashboard in Excel
- Insert Image in Excel Cell
- How to Insert New Worksheet In Excel?
- Create a Dashboard in Excel
- Page Numbers in Excel
Excel can import and export many different file types aside from the standard .xslx format. If your data is shared between other programs, like a database, you may need to save data as a different file type or bring in files of a different file type.
Export Data
When you have data that needs to be transferred to another system, export it from Excel in a format that can be interpreted by other programs, such as a text or CSV file.
- Click the File tab.
- At the left, click Export.
- Click the Change File Type.
- Under Other File Types, select a file type.
- Text (Tab delimited): The cell data will be separated by a tab.
- CSV (Comma delimited): The cell data will be separated by a comma.
- Formatted Text (space delimited): The cell data will be separated by a space.
- Save as Another File Type: Select a different file type when the Save As dialog box appears.
The file type you select will depend on what type of file is required by the program that will consume the exported data.
- Click Save As.
- Specify where you want to save the file.
- Click Save.
A dialog box appears stating that some of the workbook features may be lost.
- Click Yes.
Import Data
Excel can import data from external data sources including other files, databases, or web pages.
- Click the Data tab on the Ribbon..
- Click the Get Data button.
Some data sources may require special security access, and the connection process can often be very complex. Enlist the help of your organization’s technical support staff for assistance.
- Select From File.
- Select From Text/CSV.
If you have data to import from Access, the web, or another source, select one of those options in the Get External Data group instead.
- Select the file you want to import.
- Click Import.
If, while importing external data, a security notice appears saying that it is connecting to an external source that may not be safe, click OK.
- Verify the preview looks correct.
Because we’ve specified the data is separated by commas, the delimiter is already set. If you need to change it, it can be done from this menu.
- Click Load.
FREE Quick Reference
Click to Download
Free to distribute with our compliments; we hope you will consider our paid training.
Excel Import Data (Table of Contents)
- Import Data In Excel
- How To Import Data In Excel?
Import Data In Excel
Microsoft has provided some functions that can be used to import the data from different sources and connect them with excel. We can import the data from different sources such as from Access, Web, Text, SQL Server or any other database. We can connect Microsoft Excel with many sources and import the data in many ways.
How To Import Data In Excel?
Let’s see how to import data in Excel by using some examples:
You can download this Import Data in Excel Template here – Import Data in Excel Template
Import Data In Excel – Example #1
Many times we faced a situation where we need to extract or import the data from other sources. And in Excel, we can use the function Get External Data to import the required fields to work from different sources.
We can extract the data in excel by going in the Data menu tab, under getting and Transform data, select any required source as shown below.
Those options are shown in the below screenshot.
There are many different ways to import data in excel.
- From Text/CSV: Text files whose data can be separated with a tab or excel columns.
- From Web: Any website whose data can be converted to excel tables.
- From Table/Range: Create a new query linked to the selected Excel table or named range.
Above mentioned ways of importing the data are majorly used. There are some other ways also, as shown in the below screenshot. For that, click on From SQL Server Database under From Database section. Once we do that, we will get a drop-down list of all the other sources from where we can import the data.
Once we import the data from any server or database, the connection gets established permanently till we remove it manually. And in the future, we can import the data from previously established and connected sources to save time. For this, go to the Data menu tab; under Get & Transform Data, select Existing Connections as shown below.
Once we click on it, we will get a list of all existing connections which we link with our computer or excel or network, as shown below. From that list, select the required connection point and click on Open to access that database.
Now let’s import some data from any website. For that, we must have the link to that website from where we need to import the data. Here, for example, we have considered the link of Wikipedia as given below.
Link: https://en.wikipedia.org/wiki/Fortune_India_500
Now, go to the Data menu; under Get & Transform Data section, select From Web as shown below.
Once we do that, we will get a Web Query box. There at the address tab, copy and paste the link and click on Go as shown below.
It will then take us to the web page of that link, as shown below. Now click on the Import button to import the data.
The Import Data box will appear, asking for a reference cell or sheet to import the data. Here we have select cell A1 as shown below. And click on, OK.
Then the query will run, and it will fetch the data from the provided link as shown below.
Import Data In Excel – Example #2
Let’s see another way of importing the data. For this, we have some sample data in Notepad (or Text) file, as shown below. As we can see, the data has spaces between the lines like columns in excel has.
Importing such data is easy as it is already separated with spaces. Now to import the data in the text file, go to the Data menu tab, under the getting and Transform data section, select From Text option as shown below.
After that, we will get a box where it will ask us to browse the file. Search and select the file and click on the Import button to process further.
Just after that, a text import wizard will appear, which will ask us to delimit or separate the text. Select the Delimited option for a proper way to separate the data into columns or select the Fixed Width option to do it manually. Here we have selected Delimited as shown below.
Here we have delimited the text with Tab as shown below. Now click on Next to complete the process.
Now select the way we want to import the data. Here we have selected cell A1 for the reference point and click on OK.
We will see our data imported to excel from the selected file.
Like this, we can import the data from different sources or databases to excel to work on them.
Pros of Importing Data in Excel
- By importing the data from any other sources, we can link different tables, files, columns, or sections and get the data we require.
- It saves time by manually copying and pasting the heavy data from one source to excel.
- We also avoid unnecessary crashing or handing of files if the data is huge.
Cons of Importing Data in Excel
- Sometimes processing time is huge.
Things to Remember
- Make sure you are connected to a good internet connection when you are importing data from SQL Servers or similar databases.
- Do not do anything while data is being imported from the source files or database. It may crash the opened files.
- Always protect the previously established connection with a password.
Recommended Articles
This has been a guide to Importing Data in Excel. Here we discussed how to import data in excel along with practical examples and a downloadable excel template. You can also go through our other suggested articles to learn more –
- Auto Numbering in excel
- Watermark in Excel
- Enable Macros in excel
- Excel Uppercase Function
How do I import data from one excel file to another by using a prompt method. Having the user select the path file, or typing in the excel workbook name. Current code with no success. Please advise.
' Get customer workbook...
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook
' make weak assumption that active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook
' get the customer workbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please Select an input file "
customerFilename = Application.GetOpenFilename(filter, , caption)
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
' assume range is A1 - C10 in sheet1
' copy data from customer to target workbook
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)
targetSheet.Range("A1", "C10").Value = sourceSheet.Range("A1", "C10").Value
' Close customer workbook
customerWorkbook.Close
asked Nov 27, 2017 at 18:01
1
Here is a public function that uses the filedialog to allow the user to browse and select the file you want to manipulate in your code. The return value will be the filepath of the chosen file. It is currently set to only allow a single selection .AllowMultiSelect = False
.
Public Function GetFilePath(strTitle) As String
Dim fso As Object
Dim varFile As Variant
Dim strPath As String
On Error GoTo PROC_ERR
Set fso = Application.FileDialog(3)
With fso
.Title = strTitle
.AllowMultiSelect = False
.Show
End With
For Each varFile In fso.SelectedItems
strPath = varFile
Next
Set fso = Nothing
GetFilePath = strPath
Exit Function
PROC_ERR:
MsgBox "Error: " & Err.Number & "; " & Err.Description
GetFilePath = "Null"
End Function
So instead of:
' get the customer workbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please Select an input file "
customerFilename = Application.GetOpenFilename(filter, , caption)
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
Use:
Set customerWorkbook = Application.Workbooks.Open(GetFilePath("Select the customer file"))
answered Nov 27, 2017 at 18:22
This code will give the user a prompt to browse and select the workbook they want to import. It will import data from the worksheet (in this case Sheet1) in the selected workbook and import into a worksheet in your current workbook (that has the same name Sheet1). It will copy the range you set (in this example A1:Z100) and paste it starting with A1 in your current workbook.
Sub Get_Data_From_File ()
Dim FileToOpen As Variant
Dim OpenBook As Workbook
Application.ScreenUpdating = False
FileToOpen = Application.GetOpenFileName(Title: "Browse for your file & import",
Filefilter:="Excel Files (*.xls*),*xls*")
If FileToOpen <> False Then
Set OpenBook = Application.Workbooks.Open(FileToOpen)
Openbook.Worksheets("Sheet1").Range("A1:Z100").Copy
ThisWorkbook.Worksheets("Sheet1").Range("A1").PasteSpecial xlPasteValues
OpenBook.Close False
End If
Application.ScreenUpdating = False
End Sub
answered Aug 24, 2021 at 0:22