AutoFilter Method

Microsoft Office Web Components Object Model

AutoFilter Method

       

Displays or hides the AutoFilter drop-down arrows. You typically use this method to turn on the AutoFilter and add one or more criteria to it; you can then use the Apply method to apply the new filter.

expression.AutoFilter

expression   An expression that returns a Range object.

Remarks

Do not confuse this method with the AutoFilter property. This method applies to a Range object and turns on the AutoFilter, whereas the AutoFilter property returns the AutoFilter object for a given worksheet.

Example

This example turns on the AutoFilter for the range A1:C20, sets filters for columns A and C, and then applies the filters.

Sub Apply_AutoFilter()
    Dim afFilters
    Dim afCol1
    Dim afCol3
    
    ' Turn on AutoFilter.
    Spreadsheet1.Worksheets("Sheet1").Range("A1:C20").AutoFilter
    
    ' Set a variable to the AutoFilter object.
    Set afFilters = Spreadsheet1.Worksheets("sheet1").AutoFilter
    
    Set afCol1 = afFilters.Filters(1)
    Set afCol3 = afFilters.Filters(3)
    
    ' Add a criteria that excludes blue from column A.
    afCol1.Criteria.Add "blue"
    
    ' Add a criteria that excludes green from column A.
    afCol1.Criteria.Add "green"
    
    ' Add a criteria that excludes yellow from column c.
    afCol3.Criteria.Add "yellow"
    
    ' Apply the criteria.
    afFilters.Apply
End Sub