XL: How to Display Top 10 Records With a Macro

Last reviewed: February 27, 1998
Article ID: Q141771
The information in this article applies to:
  • Microsoft Excel for Windows 95, version 7.0
  • Microsoft Excel 97 for Windows

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 has a column of names and a column containing each person's total sales for the month, you can use the AutoFilter to show the records for the top ten salespeople, based on their sales amounts for the month. The ability to display the top ten items in a list is a new feature to the AutoFilter in Microsoft Excel versions 7.0 and 97. It is added to the drop- down lists that appear in all columns when you turn on the AutoFilter, but it can only be used on columns that contain numeric values.

You can customize the Top Ten feature in AutoFilter to filter a different number of top items in your list, such as the top 3 or top 100. This feature can be modified to display between 1 and 500 records. To manually change the number of items that appear in your AutoFilter top ten list to a number less than or greater than ten (for example, if you want to show the top five items), click the arrow in the column you want to filter, click Top 10 in the drop-down list, and then change the number of items in the Top Ten AutoFilter dialog box to the number of items you want to be displayed.

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

Use the following sample data for each of the sample macros given below:

   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

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. This Visual Basic procedure is provided 'as is' and Microsoft does not guarantee that it can be used in all situations. Microsoft does not support modifications of this procedure to suit customer requirements for a particular purpose.

Macro to Display the Top Ten Items

Because the sample list above has only five records total, this sample macro will filter 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, your list will display rows 4 and 6 only.

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

Macro to Turn Off the AutoFilter

The following macro will turn off the AutoFilter, and all of the records in the list will be displayed. Note, if the AutoFilter is not on when this macro is run, it will be enabled, but because there is no criteria specified, all records will be visible.

Sub Clear_Filter()

   Range("A1").AutoFilter

End Sub

Macro to Display the Bottom Ten Items

When you click "Top 10" from 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 will filter 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, your list will display rows 3 and 5 only.

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

REFERENCES

For more information about the AutoFilter, click Answer Wizard on the Help menu and type:

   tell me about the AutoFilter


Additional query words: 7.00 8.00 97
Keywords : kbcode kbprg xlvbahowto
Version : WINDOWS:7.0,97
Platform : WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 27, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.