Microsoft KB Archive/152215

= How to Check Number of Records Returned in an AutoFilter =

Article ID: 152215

Article Last Modified on 10/10/2006

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition
 * Microsoft Excel 95 Standard Edition
 * Microsoft Excel 5.0 Standard Edition
 * Microsoft Excel 98 for Macintosh

-



This article was previously published under Q152215





SUMMARY
When you apply an AutoFilter, Microsoft Excel displays the number of records that are filtered in the lower-left corner of the status bar.

This article contains a sample Microsoft Visual Basic for Applications macro (Sub procedure) that determines how many records were returned in a list that has AutoFilter turned on. You can also use the Subtotal function to achieve a similar result. Both methods are detailed in the "More Information" section of this article.



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.

Using a Visual Basic Macro
  Insert a module sheet and type the following code: Sub Filter_Return Sheets("sheet1").Select Range("a1").Select Selection.CurrentRegion.Select row_count = Selection.Rows.Count - 1    ' Count the rows and ' subtract the header.

' The following three lines run an AutoFilter using "Cat" as the ' criteria for the first column and greater than 0 as the ' criteria for the second column. Selection.AutoFilter Selection.AutoFilter Field:=1, Criteria1:="Cat" Selection.AutoFilter Field:=2, Criteria1:=">0" matched_criteria = 0                        ' Set variable to                                                 ' zero. check_row = 0                               ' Set variable to                                                 ' zero. While Not IsEmpty(ActiveCell)           ' Check to see if row ' height is zero. ActiveCell.Offset(1, 0).Select If ActiveCell.RowHeight = 0 Then check_row = check_row + 1 Else matched_criteria = matched_criteria + 1 End If   Wend

If row_count = check_row Then           ' If these are equal, ' nothing was returned. MsgBox "no matching data" Else MsgBox matched_criteria - 1         ' Display the number ' of records returned. End If

End Sub   Type the following information in a worksheet:       A1:  Animal     B1:  In Stock         C1:  Price A2: Dog        B2:  1                C2:  $1.00 A3: Cat        B3:  2                C3:  $2.00 A4: Dog        B4:  3                C4:  $3.00 A5: Cat        B5:  4                C5:  $4.00 A6: Bird       B6:  5                C6:  $5.00   Run the macro.

Microsoft Excel 97 and Microsoft Excel 98
On the Tools menu, point to Macro, and then click Macros. In the Macro box, select Filter_Return, and then click Run.

Microsoft Excel versions 5.0 and 7.0
On the Tools Menu, click Macro. In the Macro box, select Filter_Return, and then click Run. 

The macro displays a message box that states the number of records returned or, if no data is returned, that there is "no matching data." In the example given, the message box returns the number 2.

Using the Subtotal Function
 Use the same sample data from step 2, above. If the AutoFilter is currently on, point to Filter on the Data menu, and then click AutoFilter.  In cell C8, type the following formula: =SUBTOTAL(3,C2:C6) NOTE: The first argument for the Subtotal function is the function used to calculate the subtotal. The argument in this example uses the Count function (3) to calculate the subtotal. </li> Select cell A1, point to Filter on the Data menu, and then click AutoFilter.

This turns on the AutoFilter, and you get a drop-down box for each of the three fields in this example.</li> Click the drop-down box in the Animal field, and then click Cat in the drop-down list.

The AutoFilter filters all the records with Cat in the Animal field, and in this example, the result of the Subtotal function in cell C8 is 2.</li></ol>

Additional query words: copy paste visual basic sub total XL98 XL97 XL7 XL5 XL

Keywords: kbdtacode kbhowto kbprogramming KB152215

-

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

© Microsoft Corporation. All rights reserved.