|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
Transform Staid Text Files into Automatic Custom Reports
By David H. Ringstrom, CPA
Data arrives on your desk from many sources, but often it's in the form of a lowly text file. As you're aware, it's easy to open such files in Excel and then manually manipulate the data into the desired format. No big deal for a quick analysis, but let's say you need to process the same file in the same fashion each week or month. You could record a macro to automate many of the steps, but there's an easier alternative available: Microsoft Query. Microsoft Query has resided in the background of Microsoft Office for many years. In this article I'm going to describe how you can query text files to automatically carry out these tasks without any subsequent intervention:
Expert tip: Although this article is specific to accessing data from text files, you can easily adapt the techniques I describe to query data from databases and other sources. Start with Sample Data To begin, you first need some sample data to work with. Your data should be in text file format, and the first record should include headings. You can make up some sample data easily in a blank Excel workbook:
Data entry trick: Ctrl-Enter copies whatever you typed to the selected cells at once, eliminating the need to drag the fill handle down.
Excel 2007: Choose Fill from the Editing section of the Home ribbon, and then choose Series. Excel 2003 and earlier: Choose Edit, Fill, and then Series.
Excel 2007: Click the Office button, choose Save As, and then Other formats. Choose CSV (Comma Delimited) (*.csv) from the Save As Type drop down list, assign a name to your file, and then click Save. Excel 2003 and earlier: Choose File, and then Save As. Choose CSV (Comma Delimited) (*.csv) from the Save As Type drop down list, assign a name to your file, and then click Save.
You now have a sample data file that you can use to test the remaining procedures in this article. Create a Query The next steps involve establishing a connection to your data.
Excel 2007: Choose From Other Sources in the Data section of the ribbon, and then select From Microsoft Query. Excel 2003 and earlier: Choose Data, Import External data, and then New Database query. Installation Required? Depending upon how Microsoft Office was installed on your computer, you may be prompted to insert your Office CD so that Microsoft Query can be installed. This is a one-time step.
· Customer · Salesperson · City · Sales Excel 2003 and earlier: A Query Wizard will likely walk you through the first few steps that I describe here. Once you've completed the wizard then you'll have a choice to View Data or Edit Query in Microsoft Query. At that point you'll see the same dialog box that I describe for Excel 2007 users. As you can see, one of the benefits of using Microsoft Query in this fashion is that you can reorder the data to your liking.
a. Choose Records, and then Sort. b. Choose Sales from the column field, specify Descending, and then click Add. c. Click Close to return to the main Microsoft Query window. Multiple sorts: If you have a text file with many different columns of data, you can add as many sorts as you wish.
a. Click Criteria, and then Add Criteria. b. Choose City from the Field list. c. Click the Values button, and then choose Memphis. d. Click Add. e. Optionally add additional criteria, such as a specific salesperson's name. f. Click Close to return to the main Microsoft Query window. Explore the SQL
statement At this point we're ready to return our data to Excel, but before we do, let's take a moment to look at the underlying SQL statement that Microsoft Query created as we made the preceding choices. SQL stands for Structured Query Language, and is a common method for extracting data from databases and other sources.
SELECT Instructs Microsoft Query which fields we wish to return from the data source. FROM: Tells Microsoft Query which data source we're using. WHERE: Instructs Microsoft Query that we only want records where the city is equal to Memphis. ORDER BY: This informs Microsoft Query that we wish to sort the data in descending order based on the Sales field.
If you have experience with SQL statements then you can create some very robust queries. Of course, if you'd rather not create SQL statements, simply use the menus within Microsoft Query instead. Learn more: Free SQL statements tutorials are available at www.w3schools.com/sql and www.sqlcourse.com. However, there is one tweak to the query that you can't make through menu picks, which is to limit the query to the top 5 choices. To do so, view the SQL statement as described above, and then insert the words TOP x where x is the number of records that you wish to return, as shown in Figure 7. When you click the SQL window, click OK on the prompt that says the SQL Query Can't Be Represented Graphically.
Return the Data to
Excel You're now ready to return the data you queried from the text file to your Excel worksheet:
As you can see in Figure 10, Excel 2007 automatically formats your data as a table, which means it automatically formats the rows. You can easily eliminate this formatting:
If you'd rather type your own field names in at the top of the table, as opposed to those from the text file, unclick the Header Row checkbox in the Design tab of the ribbon. To do so in Excel 2003 or earlier, right-click on the table, choose Data Range Properties, and then clear the checkbox for Include Field Names.
Design tab tip: The Design tab is context sensitive, meaning it disappears if you click outside of the data that has been returned from the text file. Refresh or Edit Your
Query If you set the Refresh Data When Opening File option, then your data will automatically refresh when you open your file. Alternatively, you can right-click anywhere within the data, and then choose Refresh. You'll want to refresh the data any time that you've received a new version of the corresponding text file. You can also edit the query: Excel 2007: Click the Properties button in the External Table Data section of the Design ribbon. Excel 2003 and earlier: Right-click on the table and choose Edit Query. David Ringstrom heads an Atlanta-based software and database consulting firm serving clients nationwide since 1992. Long ago David recognized that either you work Excel, or it works you, so much of his work involves creating Excel-based tools to streamline accounting and other processes. David shares his knowledge with others as a freelance writer, professional speaker and technical editor, and is proud to have served in the United States Navy. David can be reached at david@acctadv.com The views and opinions expressed in this column are those of the author and do not necessarily reflect the opinions of Microsoft. |
|||||||||||||||||||||||||||||||||||||||||||||||||
Printer-friendly version |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||