Suggestions |
Sample file
Autofilter
|
1 Applies to 2 The filtering result 2.1 Mark the filtered rows 2.2 Copy the filtered rows 2.3 Move the filtered rows 2.4 Delete the filtered rows 3 Filtercriteria 3.0 The arguments of Autofilter 3.1 TEXT 3.2 NUMBERS 3.3 DATES 3.3.1 Day 3.3.2 Week 3.3.3 Month 3.3.4 Quarter 3.3.5 Year 3.3.6 Specific month 3.4 COLOUR 3.5 ICON 3.5.1 Iconset 1 3.5.2 Iconset 19 3.5.3 Iconset 9 3.5.4 Iconset 14 3.5.5 Iconset 2 3.5.6 Iconset 10 3.5.7 Iconset 15 3.5.8 Iconset 4 3.5.9 Iconset 6 3.5.10 Iconset 11 3.5.11 Iconset 5 3.5.12 Iconset 13 3.5.13 Iconset 7 3.5.14 Iconset 3 3.5.15 Iconset 8 3.5.16 Iconset 18 3.5.17 Iconset 17 3.5.18 Iconset 20 3.5.19 Iconset 12 3.5.20 Iconset 16 |
Autofilter is one of Excel's most used methods. The method has been expanded in the course of time. The documentation in VBA is partially lacking. This page describes the autofilter options in Excel 2010. An autofilter applies to a column: a whole column or a part of it. You can consider columns as 'fields' in a table; 'to filter a column' is equivalent to 'filtering a field'. sheet1.columns(3).autofilter 1, ... sheet1.usedrange.columns(3).autofilter 1, .... sheet1.usedrange.autofilter 3, .... sheet1.cells(1).currentregion.columns(3).autofilter 1, ... sheet1.cells(1).currentregion.autofilter 3, ... selection.columns(3).autofilter 1, ... selection.autofilter 3, ... sheet1.Listobjects(1).range.columns(3).autofilter 1, ... sheet1.Listobjects(1).range.autofilter 3, ... sheet1.Listobjects(1).databodyrange.columns(3).autofilter 1, ... sheet1.Listobjects(1).databodyrange.autofilter 3, ... If you need to filter several columns you will have to create several filters that will be executed sequentially. These filters act as "AND" filters: only rows that will match both (or more) criteria will be shown. Autofilter treats the first row of a filtered range as headin / 'fieldnamerange'. Autofilter ignores the first row in the filtering and this row will always be visible. That effects method like copy, mark, delete, etc. The autofilter hides all rows that do not match the filter criteria. Most of the time the result consists of non-contiguous visible rows. You can copy those visible data to another location in the worksheet or in the workbook. The copied data will be converted automatically into a contiguous range of data. You can use that range for validation purposes or to populate a combobox/listbox. Mark the filtered (visible) rows by an interior colour. With cells(1).currentregion
.autofilter 3, "aa17"
End With.interior.colorindex=3 .autofilter To avoid this, use: With cells(1).currentregion
.autofilter 3, "aa17"
End With.offset(1).interior.colorindex=3 .autofilter To avoid this, use e.g.: With cells(1).currentregion
.autofilter 3, "aa17"
End With.offset(1).resize(.rows.count-1).interior.colorindex=3 .autofilter With cells(1).currentregion
.autofilter 3, "aa17"
End With.copy sheet2.cells(1) .autofilter The method 'copy' copies the heading row automatically. To avoid this, use: With cells(1).currentregion
.autofilter 3, "aa17"
End With.offset(1).copy sheet2.cells(1) .autofilter 2.3 Move the filtered rows:cut & paste The methode Range.Cut can't manipulate non-contiguous cells.This method can't be applied in combination with Autofilter. The method Rows.Delete can handle non-contiguous cells. With cells(1).currentregion
.autofilter 3, "aa17"
End With.entirerow.delete .autofilter To avoid that, use e.g.: With cells(1).currentregion
.autofilter 3, "aa17"
End With.offset(1).entirerow.delete .autofilter Below you'll find VBA-code as examples of all different methods and conditions in Autofilter. The code uses a table (VBA Listobject) in sheet1 (see the sample file). The second column consists of strings. The third column contains dates. The fourth column houses numbers. 3.0 The arguments of Autofilter expression.AutoFilter(Field, Criteria1, Operator, Criteria2, VisibleDropDown)Field: the number of the column to which the filter applies. Criteria1: the first condition/criterion for the filtering. Operator: a number or named constant that modifies the filtercondition(s). Criteria2: an optional second condition/criterion for filtering the same column/field. VisibleDropDown: show/hide the filtericon Text is empty: Sheet1.ListObjects(1).Range.AutoFilter 2, "=" Sheet1.ListObjects(1).Range.AutoFilter 2, "<>" Sheet1.ListObjects(1).Range.AutoFilter 2, "Raisin" Sheet1.ListObjects(1).Range.AutoFilter 2, "Raisin", 7 Sheet1.ListObjects(1).Range.AutoFilter 2, "Pork", 2, "Quiche" Sheet1.ListObjects(1).Range.AutoFilter 2, Array("Pork", "Quiche"), 7 Sheet1.ListObjects(1).Range.AutoFilter 2, [transpose(G1:G2)], 7 Sheet1.ListObjects(1).Range.AutoFilter 2, "Pork", 1, "Quiche" Sheet1.ListObjects(1).Range.AutoFilter 2, "<>Raisin" Sheet1.ListObjects(1).Range.AutoFilter 2, "Berry*" Sheet1.ListObjects(1).Range.AutoFilter 2, "<>Berry*" Sheet1.ListObjects(1).Range.AutoFilter 2, "*Berry" Sheet1.ListObjects(1).Range.AutoFilter 2, "<>*Berry" Sheet1.ListObjects(1).Range.AutoFilter 2, "*Berry*" Sheet1.ListObjects(1).Range.AutoFilter 2, "*Be??y*" Sheet1.ListObjects(1).Range.AutoFilter 2, "<>*Berry*" Sheet1.ListObjects(1).Range.AutoFilter 4, 146 Sheet1.ListObjects(1).Range.AutoFilter 4, "<>146" Sheet1.ListObjects(1).Range.AutoFilter 4, ">146" Sheet1.ListObjects(1).Range.AutoFilter 4, ">=146" Sheet1.ListObjects(1).Range.AutoFilter 4, "<146" Sheet1.ListObjects(1).Range.AutoFilter 4, "<=146" Sheet1.ListObjects(1).Range.AutoFilter 4, "<=146", 1, ">=100" Sheet1.ListObjects(1).Range.AutoFilter 4, , 3 You can specify how many highest values you want to be shown. You can enter that amount in the argument 'Criteria1'; the operator for the filtering stays unchanged: 3. Doing so you can filter the highest 3 values or the highest 25 vaules using the operator 'xltop10items'. Number top 12 items: Criteria1 = 12 operator 3 (xltop10items) Sheet1.ListObjects(1).Range.AutoFilter 4,12 , 3 Sheet1.ListObjects(1).Range.AutoFilter 4, , 4 You can specify how many of the lowest values you want to be shown. You can enter that amount in the argument 'Criteria1'; the operator for the filtering remains unchanged: 4. Doing so you can filter the lowest 3 values or the lowest 25 values using the operator 'xlbottom10items'. Number bottom 12 items: Criteria1 = 12 operator 4 (xlbottom10items) Sheet1.ListObjects(1).Range.AutoFilter 4,12 , 4 Sheet1.ListObjects(1).Range.AutoFilter 4, , 5 You can specify which percentage of the highest values you want to be shown. You can enter that amount in the argument 'Criteria1'; the operator for the filtering remains unchanged: 5. Doing so you can filter 3 percent of the highest values or 25 percent of the highest values using the operator 'xltop10percent'. Number top 12 percent items: Criteria1 = 12 operator 5 (xltop10percent) Sheet1.ListObjects(1).Range.AutoFilter 4,12 , 5 Sheet1.ListObjects(1).Range.AutoFilter 4, , 6 You can specify which percentage of the lowest values you want to be shown. You can enter that amount in the argument 'Criteria1'; the operator for the filtering remains unchanged: 6. Doing so you can filter 3 percent of the lowest values or 25 percent of the lowest values using the operator 'xlbottom10percent'. Number bottom 12 percent items: Criteria1 = 12 operator 6 (xlbottom10percent) Sheet1.ListObjects(1).Range.AutoFilter 4,12 , 6 Sheet1.ListObjects(1).Range.AutoFilter 4, 33, 11 Sheet1.ListObjects(1).Range.AutoFilter 4, 34, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, "=" & Date - 1 Sheet1.ListObjects(1).Range.AutoFilter 3, "<>" & 1 * Date - 1 Sheet1.ListObjects(1).Range.AutoFilter 3, "<" & 1 * Date Sheet1.ListObjects(1).Range.AutoFilter 3, ">" & 1 * Date Sheet1.ListObjects(1).Range.AutoFilter 3, ">" & 1 * Date - 30, 1, "<" & 1 * Date + 30 Sheet1.ListObjects(1).Range.AutoFilter 3, 1, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 2, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 3, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 4, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 5, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 6, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 7, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 8, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 9, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 10, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 11, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 12, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 13, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 14, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 15, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 16, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 21, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 22, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 23, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 24, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 25, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 26, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 27, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 28, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 29, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 30, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 31, 11 Sheet1.ListObjects(1).Range.AutoFilter 3, 32, 11 Enter the filtercolour in argument Criteria1 by - number, - RGB(12, 23, 56) , - &HC6FF00 or - the property of an object e.g. Cells(1).Interior.Color Sheet1.ListObjects(1).Range.AutoFilter 4, 8176239, 8 Sheet1.ListObjects(1).Range.AutoFilter 4, rgb(23,46,213), 8 Sheet1.ListObjects(1).Range.AutoFilter 4, &HC6FF00, 8 Sheet1.ListObjects(1).Range.AutoFilter 4, Cells(1).Interior.Color, 8 Sheet1.ListObjects(1).Range.AutoFilter 4, 8176239, 9 Sheet1.ListObjects(1).Range.AutoFilter 4, rgb(23,46,213), 9 Sheet1.ListObjects(1).Range.AutoFilter 4, &HC6FF00, 9 Sheet1.ListObjects(1).Range.AutoFilter 4, Cells(1).Font.Color, 9 Sheet1.ListObjects(1).Range.AutoFilter 4, , 12 Sheet1.ListObjects(1).Range.AutoFilter 4, , 13 Sheet1.ListObjects(1).Range.AutoFilter 4, , 14 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(1).Item(1), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(1).Item(2), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(1).Item(3), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(19).Item(1), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(19).Item(2), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(19).Item(3), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(9).Item(1), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(9).Item(2), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(9).Item(3), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(9).Item(4), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(14).Item(1), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(14).Item(2), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(14).Item(3), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(14).Item(4), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(14).Item(5), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(2).Item(1), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(2).Item(2), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(2).Item(3), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(10).Item(1), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(10).Item(2), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(10).Item(3), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(10).Item(4), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(15).Item(1), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(15).Item(2), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(15).Item(3), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(15).Item(4), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(15).Item(5), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(4).Item(1), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(4).Item(2), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(4).Item(3), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(6).Item(1), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(6).Item(2), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(6).Item(3), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(11).Item(1), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(11).Item(2), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(11).Item(3), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(11).Item(4), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(5).Item(1), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(5).Item(2), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(5).Item(3), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(13).Item(1), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(13).Item(2), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(13).Item(3), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(13).Item(4), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(7).Item(1), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(7).Item(2), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(7).Item(3), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(3).Item(1), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(3).Item(2), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(3).Item(3), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(8).Item(1), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(8).Item(2), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(8).Item(3), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(18).Item(1), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(18).Item(2), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(18).Item(3), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(17).Item(1), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(17).Item(2), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(17).Item(3), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(17).Item(4), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(17).Item(5), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(20).Item(1), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(20).Item(2), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(20).Item(3), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(20).Item(4), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(20).Item(5), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(12).Item(1), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(12).Item(2), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(12).Item(3), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(12).Item(4), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(16).Item(1), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(16).Item(2), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(16).Item(3), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(16).Item(4), 10 Sheet1.ListObjects(1).Range.AutoFilter 4, ThisWorkbook.Iconsets(16).Item(5), 10 |