Microsoft Excel Help: Use Query To Get A Unique Set Of Records

Microsoft Excel Help: Use Query To Get A Unique Set Of Records
Page content

See all Microsoft Excel tips

Strategy:

I have to bring this up, because it is fast and fairly cool. It is also a way to get a list of customers from a closed file. The trick is to use Microsoft Query. Now, Microsoft Query is not in the default install of Excel. So, if you didn’t do a complete install, you are going to have to find the installation CDs to add Microsoft Query to your installation of Excel.

Basically, Microsoft Query is a way to run some SQL against an external file. I am sure that Microsoft envisioned this would be an Access table or an ODBC datasource, but, of course, someone figured out that the external file could be another Excel workbook. Here is how you would set this up.

  1. Let’s say you have a workbook called SalesData.xls. It has one worksheet called Data. The worksheet contains a bunch of records in columns A through H, as shown in Fig. 1003. Select the data in A:H. Give the table a range name of MyData.

  2. Save and Close SalesData.xls.

  1. In a new workbook, select Data – Get External Data – New Database Query, as shown in Fig. 1004.

  2. In the Choose Data Sources dialog, choose Excel Files, as shown in Fig. 1005. Choose OK.

  3. Laugh at the retro Select Workbook dialog. Then, browse to find SalesData.xls, as shown in Fig. 1006. Choose OK.

  4. As shown in Fig. 1007, select the Customer field in the left list and hit the Right Arrow button to move Customer over to the Query.

  5. Choose the Next button, as shown in Fig. 1008.

  1. Choose Next to skip the Filter section. See Fig. 1009.

  2. Choose to Sort Ascending by Customer, as shown in Fig. 1010. Click Next.

  3. Since you want unique records only, you need to edit the query in Microsoft Query. As shown in Fig. 1011, choose this option and then choose Finish.

  4. The Microsoft Query window opens. From the Microsoft Query menu, select View – Query Properties, as shown in Fig. 1012.

  5. As shown in Fig. 1013, choose Unique Values Only and then OK.

  1. As shown in Fig. 1014, select File – Return Data to Microsoft Excel.

  2. Choose where you would like to place the imported data, as shown in Fig. 1015. Choose OK.

**

Result:

You have a unique list of customers from the closed Excel file. See Fig. 1016.

Additional Details:

Under Data – Import External Data – Data Range Properties, you can set this query up to update every time the workbook is opened, as shown in Fig. 1017.

Summary:

The Data Query tool provides a way to import data from Access, Excel, or ODBC data sources.

Commands Discussed:

Data – Import External Data – New Database Query

**

See all Microsoft Excel tips

Images

Fig. 1004

Fig. 1005

Fig. 1006

Fig. 1007

Fig. 1008

Fig. 1009

Fig. 1010

Fig. 1011

Fig. 1012

Fig. 1013

Fig. 1014

Fig. 1015

Fig. 1016

Fig. 1017