Microsoft KB Archive/185326

= XL98: How to Display Top 10 Records with a Macro =

Article ID: 185326

Article Last Modified on 6/17/2005

-

APPLIES TO


 * Microsoft Excel 98 for Macintosh

-



This article was previously published under Q185326



SUMMARY
In Microsoft Excel, you can use the AutoFilter feature to filter a list based on simple criteria. For example, if you have a list of salespeople that contains a column of names and a column that contains each person's total sales for the month, you can use the AutoFilter to show the records for the top ten salespeople based on sales amounts for the month. The ability to display the top ten items in a list is a new feature in Microsoft Excel 98. It is added to the drop-down lists that appear in all columns when you turn on the AutoFilter. However, it can be used only for columns that contain numeric values.

You can customize the Top Ten feature in AutoFilter to filter a different number of top items in a list, such as the top 3 or top 100. You can modify this feature to display between 1 and 500 records. To change the number of items in the AutoFilter top ten list, click the arrow in the column you want to filter, click Top 10 in the list, and then change the number of items in the Top Ten AutoFilter dialog box.

This article provides several examples of Visual Basic for Applications macro code that you can use to control the AutoFilter feature in Microsoft Excel.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. Use the following sample data for each of the following sample macros:   A1: Name   B1: Zone   C1: Amount A2: Bob   B2: a      C2: 3 A3: Sue   B3: c      C3: 2 A4: Mary  B4: a      C4: 6 A5: Pete  B5: b      C5: 1 A6: Paul  B6: b      C6: 4

Macro That Displays the Top Ten Items
Because the sample list has a total of only five records, this sample macro filters the top two items in the list based on the Amount field: Sub Top_Ten

Range("A1").AutoFilter Field:=3, Criteria1:="2",

Operator:=xlTop10Items End Sub If you run this macro from the sheet that contains the sample list, the list displays only rows 4 and 6.

NOTE: You can filter more (or less) than 10 items by using the Top Ten feature. To change the number of items that appear when you run this macro, set the Criteria1 argument to the number you want.

Macro That Turns Off AutoFilter
The following macro turns off the AutoFilter feature and displays all of the records in the list. Note that if the AutoFilter feature is not selected when this macro is run, it is turned on. However, because no criteria is specified, all records are visible. Sub Clear_Filter

Range("A1").AutoFilter

End Sub

Macro That Displays the Bottom Ten Items
When you click Top 10 in the AutoFilter list, the default setting is the top 10 items in the column. However, in the Top 10 AutoFilter dialog box, you can choose to display the Bottom 10 items instead.

The following macro filters the bottom two items from the list based on the Amount field: Sub Bottom_Ten

Range("A1").AutoFilter Field:=3, Criteria1:="2", _ Operator:=xlBottom10Items

End Sub If you run this macro from the sheet that contains the list, the list displays only rows 3 and 5.

NOTE: You can filter more (or less) than 10 items by using the Top Ten feature. To change the number of items that appear when you run this macro, set the Criteria1 argument to the number you want.

Additional query words: XL98

Keywords: kbhowto kbprogramming kbdtacode KB185326

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.