ID: Q141770
The information in this article applies to:
In Microsoft Excel, you can use the AutoFilter feature to filter a list based on simple criteria. This article contains several sample Microsoft Visual Basic for Applications macros (Sub procedures) for controlling the AutoFilter feature in Microsoft Excel.
Microsoft provides programming examples 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 article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/refguide/
To test each of the sample macros in this article, use the following sample
data:
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
The following macro displays the records in the list where the items in the "Zone" field are either "a" or "b".
Sub Custom1()
Range("A1").AutoFilter Field:=2, Criteria1:="a", Operator:=xlOr, _
Criteria2:="b"
End Sub
If you run this macro from the sheet that contains the list, your list will
show all records, except row three.
The following macro turns off the AutoFilter, assuming it is enabled, and displays all the records in the list.
NOTE: If the AutoFilter is not on when this macro is run, it will be enabled, but since there is no criteria specified, all records should still appear
Sub Clear_Filter()
Range("A1").AutoFilter
End Sub
The following macro displays the records in the list where the items in the
"Amount" field are greater than or equal to 1, but less than 4.
Sub Custom2()
Range("A1").AutoFilter Field:=3, Criteria1:=">=1", _
Operator:=xlAnd, Criteria2:="<4"
End Sub
If you run this macro from the sheet that contains the sample list, rows 2,
3, and 5 appear.
Sub Custom3()
Range("A1").AutoFilter Field:=2, Criteria1:="a"
Range("A1").AutoFilter Field:=3, Criteria1:="3"
End Sub
If you run this macro from the sheet that contains the above list, only row
2 appears.
For more information about the AutoFilter, click the Search button in Help and type:
autofilter
Additional query words: 5.00 5.00a 5.00c 7.00 8.00 XL98 XL97 XL7 XL5
Keywords : kbprg kbdta kbdtacode PgmHowto KbVBA
Version : WINDOWS:5.0,5.0c,7.0,97; MACINTOSH:5.0,5.0a,98
Platform : MACINTOSH WINDOWS
Issue type : kbhowto
Last Reviewed: May 17, 1999