Вы находитесь на странице: 1из 4

IT Services

Microsoft Excel 2007 Microsoft Query (Level 3)

Contents
Introduction ..............................................................................................................1 Importing Data from Access into a New Query ...........................................1 Editing the Query .....................................................................................................2 Using a Drop-Down Menu to Supply a Value ...............................................4

Introduction
Microsoft Query lets you link to data held in a database and extract a subset of the data to a sheet in Excel. By providing a value in a cell or picking one from a drop-down list, the matching data is automatically passed back from the database to Excel.

Importing Data from Access into a New Query


Data can be imported from an Access (or other) database into Excel via the Import External Data command. You can also import data stored in another Excel file. 1. Start up Excel to get a new spreadsheet 2. Move to the Data tab on the Ribbon then click on [From Other Sources] in the Get External Data group (you may have a [Get External Data] button on the far left then choose From Other Sources) 3. Select From Microsoft Query

4. The Choose Data Source dialog box appears - select MS Access Database* then press <Enter> for [OK] 5. Select the required file - change Drives to d:User, open up the Training folder and choose example.accdb then click on [OK] (the file can be downloaded via the link provided here) 6. The Query Wizard now starts up:

i.

Select the students table and click on the + to display the fields - move across the first 6 fields up to and including Hall (double click or select and click on [>]) - click on [Next>]

ii. Set up a filter (none is required here) - click on [Next>] iii. Set up any sorting required (again none is required here) - click on [Next>] iv. Choose Return Data to Microsoft Office Excel to simply paste in the data (you will be looking at View data or edit query in Microsoft Query later) click on [Finish] 7. Finally, state where you would like your imported data to appear - which cell on which sheet, here choose A1 on the current empty sheet - press <Enter> or click on [OK] to finish The data appears in Excel as a table (alternatively, you could have chosen a Pivot Table or Pivot Chart at the fourth step of the wizard above). Filters are automatically provided in the cells on the header row; these also give you sorting options. These facilities may provide you with everything you need, but Microsoft Query offers extra powerful advanced features, as you will see.

Editing the Query


You can now make any amendments required by editing the query: 1. Right click inside the table and choose Table then Edit Query - the Query Wizard again appears 2. Click on [Next>] (unless you wish to add extra columns) 3. Set up a filter - click on Hall in Column to filter then use the list arrows to first select equals then choose any hall (eg Bridges) - click on [Next>] 4. Set up a sort on Surname in ascending order - click on [Next>] 5. In the final step of the Query Wizard turn on [View data or edit query in Microsoft Query] then click on [Finish]

The results of the query are shown in the bottom half of the Microsoft Query Window. The query would be of wider use if it were turned into a parameter query. To do this, you have to change the current filter Value into a question surrounded by square brackets. 6. In the Value: row, replace Bridges by [Which Hall?] 7. Close the Microsoft Query window by clicking on the close window button 8. A dialog box appears - enter a parameter value (eg Childs) then press <Enter> for [OK] The results appear on the Excel spreadsheet, as before. 2

To refresh the data or obtain a different set (as a result of supplying a different parameter): 9. Click on the [Refresh] button on the Table Tools Design tab 10. Type in another hall (eg Sibly) The Refresh button has additional facilities: 11. Click on the arrow below the [Refresh] button and choose Connection Properties

12. Note the Refresh control settings here you can ask for the data to be refreshed each time the file is opened, or every n minutes (to reflect any changes which have been made to the database) The Connection Properties dialog box also has a Definition tab have a look, if you like, but the settings are quite complex here. Note the links to [Edit Query] and [Parameters] at the bottom. Youll look at parameters next: 13. Press <Esc> or click on [Cancel] to close the Connection Properties dialog box 14. Next, move to an empty cell (eg K1) and type any character (eg press the <spacebar>) 15. Now, right click inside the table and choose Table then Parameters a dialog box appears:

16. Turn on the Get the value from the following cell: option and set the cell to K1 17. Also turn on Refresh automatically when cell value changes 18. Press <Enter> for [OK] to close the Parameters dialog box 19. Now type the name of a Hall (eg Wessex) into cell K1 and press <Enter> - the query results change to reflect the new value 20. Repeat step 19 for another hall eg Bridges 3

Using a Drop-Down Menu to Supply a Value


If you really want to be clever, you can use a drop-down menu to supply the value for the query parameter. First, you have to create a list of possible values: 1. In an empty cell (eg M1) type the number 1 - press <right arrow> 2. In cell N1, type the name of the first Hall (ie Bridges) press <Enter> 3. Move down to M2, type the number 2 then move to N2 and type another Hall (eg Bulmershe) 4. Continue filling columns M and N with numbers and the names of some Halls in alphabetical order (eg Childs, Hillside, Sibly, Wessex) You also need to display another tab on the Ribbon: 5. Click on the [Office Button] then on [Excel Options] at the foot of the menu 6. On the Popular tab, turn on Show Developer tab in the Ribbon press <Enter> for [OK] 7. Move to the new tab and, in the Controls group, click on [Insert] and choose the second control - the [Combo Box (Format Control)] 8. Now, using the mouse, draw a box covering K1 to L1 (drag across the cells) 9. Right click on the new Combo Box and choose Format Control... 10. On the Control tab, set the Input range by dragging through the Hall names typed into the cells in column N, set Cell link to L1 and Drop down lines to the number of Halls in the list 11. Press <Enter> or click on [OK] to close the Format Control window This above step stores the number of the chosen Hall in cell L1, which is hidden from view by the combo box itself. 12. Click away from the Combo Box to release it, then use its list arrow to test that it is working correctly 13. Click in cell J1 then press <right arrow> to move into K1 Remember, this cell controls the data shown in columns A to F. Instead of a fixed value, this needs to change as a new value is picked up from the combo box. This is achieved through a Lookup function: 14. Click on the [Insert Function] button on the Formula Bar 15. From the Or select a category: list choose Lookup & Reference then VLOOKUP as the Function name press <Enter> for [OK] 16. Set the Lookup_value to L1, set the Table_array by dragging through the data typed into the cells in columns M and N, set Col_index_num to 2 - press <Enter> or click on [OK] to complete the function 17. Test out the drop-down menu by choosing different Halls from the Combo Box Note: To hide the lookup list of halls and numbers, simply change the font colour in these cells to white. Alternatively, hide the columns or place the cells on a different sheet.

Trademark owned by Microsoft Corporation. Screen shot(s) reprinted by permission from Microsoft Corporation. Copyright 2009: The University of Reading Last Revised: August 2010 4

Вам также может понравиться