December 29, 2011

Importing Text Files in an Excel Sheet

Introduction
If you've ever tried to open a text file, you have probably used the Text import wizard.
If you have ever opened a CSV file, you have probably noted you can't set up how it gets imported. The Text import wizard does not start for these file types.
What if you have the situation where you get files with similar structure, which need the same text import settings over and over?
How do you import the data from those files into the same worksheet each time, without having to re-define the import settings over and over again?
I'll tell you.
Setting up which file (type) to import
First select the sheet where your data needs to appear. Note that the following steps differ for the various Excel versions.
Excel 2010/2007
On the ribbon, click the data tab and then click the "From Text" button on the "Get External data" group.
image
The "From Text" button, located on the Data tab of the ribbon
Select your file from the next dialog:
image
The import text file dialog
Click Import. (you can skip the next section about Excel 2003 and older and continue with All versions: defining the import settings)
Excel 2003 and older
From the menu, select Data, Get External Data, Import Data. The following dialog opens:
image
The select data source dialog of Excel 2003, selecting source type
In this dialog, choose the "Text files" option and then navigate to your file:
image
Select data source dialog, selecting a file
So far we've selected what to import. Now it is time to define what settings we need for the import. This section applies to all Excel versions as of Excel XP (and maybe even older). The screenshots shown here may look slightly different from yours, but the principle is the same.
Click the Open button. The Text Import Wizard opens up:
image
Step 1 of the Text Import Wizard, define file type
In this example I have selected to import a delimited file and set the file origin to Windows (ANSI). Click Next when you're happy with the settings.
Step 2 of the wizard allows us to define the delimiters. I selected Comma:
image
Step 2 of the Text Import Wizard: define delimiters
Click Next again to go into the third step, where you can select a format for each column of your file. I changed the date format of the first column to dmy order. Click a column to set up its formatting.
image
Step 3 of the wizard, Define column formats
You can click the advanced button to set up details like the decimal and thousands separators (note I switched them here):
image
The Advanced text Import Settings dialog
Note that any changes made in this dialog apply to all columns.
After you finished defining all columns, click the Finish button. Excel opens the Import Data dialog, asking where to put the results. Select the proper location.
image
The Import Data dialog
Hold your horses, don't click OK yet! Click the properties button instead. The External Data Range Properties dialog opens up. There are some very important settings to be made here!
image
The External Data Range Properties dialog
Note that I have highlighted some important features in this dialog. I'll discuss them below.
Save Query Definition
We wouldn't want to redo the settings each time, so let’s keep this one checked!
Prompt for the file name on refresh
Checking this box forces Excel to prompt you for a file name each time you hit the refresh button. Leave this box unchecked if you are importing the same file over and over.
Overwrite existing cells with new data, clear unused cells
What selection is best here depends on your needs entirely. The first two will force Excel to push existing data down and pull existing data up, depending on the size of your file.
I recommend NOT to put anything below an area where you are importing data; use a dedicated worksheet for this purpose.
Fill down formulas in columns adjacent to data
A very useful option. If you are planning to do calculations on each row, this option ensures each row of data will have your formulas.
Hit OK if you're happy with the settings.
Import your data
Finally, click OK to have your data imported. My sheet looks like this:
image
Results after importing the text file
Now that you have properly set up your import, save your file!
Refresh your data
All of this was meant to predefine an import routine you have to do over and over again. So how do you import a new file? Easy. Click any cell within your sheet that holds data of a previous import.
Next, we'll need to split this up in two sections again, because the various Excel versions differ in how it is done.
Excel 2010/2007
On the Data tab, find the group called "Connections". Click the dropdown "Refresh All" and select "Refresh:
image
The Refresh All button on the ribbon
If you have set the option to prompt for a file, Excel will open the appropriate dialog. Select your file and click Import.
Excel 2003 and older
In Excel 2003, the refresh button can be found in two places.
1. On the Data menu (you need have a cell selected within an imported table):
image
The refresh button on the data menu
2. On the External data toolbar:
image
The refresh button on the External data toolbar
So there you go, all set!
Conclusion
Importing text files into Excel can be a labor-intensive process if you're going through the same steps over and over again. With proper use of the data import options Excel offers, you can save yourself quite some work and at the same time improve consistency.

No comments :