Objeto AutoFilter

Worksheets (Worksheet)
AutoFilter
Filters (Filter)

Representa a filtragem automática para a planilha especificada.

Usando o objeto AutoFilter

Use a propriedade AutoFilter para retornar o objeto AutoFilter. Use o método Filters para retornar uma coleção de filtros de coluna individuais. Use o método Range para retornar o objeto Range, que representa todo o intervalo filtrado. O exemplo a seguir armazena o endereço e critérios de filtragem para a filtragem atual e aplica novos filtros.

Dim w As Worksheet
Dim filterArray()
Dim currentFiltRange As String

Sub ChangeFilters()

Set w = Worksheets("Crew")
With w.AutoFilter
currentFiltRange = .Range.Address
With .Filters
ReDim filterArray(1 To .Count, 1 To 3)
For f = 1 To .Count
With .Item(f)
If .On Then
filterArray(f, 1) = .Criteria1
If .Operator Then
filterArray(f, 2) = .Operator
filterArray(f, 3) = .Criteria2
End If
End If
End With
Next
End With
End With

w.AutoFilterMode = False
w.Range("A1").AutoFilter field:=1, Criteria1:="S"

End Sub

Para criar um objeto AutoFilter para uma planilha, você deve ativar a filtragem automática para um intervalo na planilha manualmente ou usando o método AutoFilter do objeto Range. O exemplo a seguir usa os valores armazenados em variáveis em nível de módulo no exemplo anterior para restaurar a filtragem automática original para a planilha Crew.

Sub RestoreFilters()
Set w = Worksheets("Crew")
w.AutoFilterMode = False
For col = 1 To UBound(filterArray(), 1)
If Not IsEmpty(filterArray(col, 1)) Then
If filterArray(col, 2) Then
w.Range(currentFiltRange).AutoFilter field:=col, _
Criteria1:=filterArray(col, 1), _
Operator:=filterArray(col, 2), _
Criteria2:=filterArray(col, 3)
Else
w.Range(currentFiltRange).AutoFilter field:=col, _
Criteria1:=filterArray(col, 1)
End If
End If
Next
End Sub