Capture an excel autofilter criterion based on cell color

Asked

Viewed 137 times

0

How to capture an Excel autofilter criterion that is based on cell color?

Code:

Sub GetCellColorCriteria()

With ActiveSheet
 If .AutoFilterMode Then
   With .AutoFilter.Filters(1)
     If .On Then
        If .Operator = 8 Then  'xlFilterCellColor = 8
           c1 = .Criteria1     '<<<< this line generate an error;
           vprov2 = RGB(255, 0, 0)
           If .Criteria1 = vprov2 Then  '<<<< this line generate an error;
            vprov = True
           End If

         End If
      End If
    End With
  End If
End With

End Sub

1 answer

0

The criteria and the operators are correct, but I believe that the autofilter is being used in the wrong way.

With the following sample data in Column D:

Dados de exemplo

Code:

Sub filtrar()
    Dim ws As Worksheet
    Dim UltimaLinhaD As Long
    ' Mudar para planilha utiliza, neste exemplo a Planilha1
    Set ws = ActiveWorkbook.Worksheets("Planilha1")
    With ws
        'Limpa os Autofiltros da Planilha para evitar erros
        If .FilterMode Then
            .ShowAllData
        End If
        'Última Linhada colunaE
        UltimaLinhaD = .Cells(.Rows.Count, "D").End(xlUp).Row
        'Autofiltro
        .Range("D1:D" & UltimaLinhaD).AutoFilter Field:=1, Criteria1:=RGB(255, 0, 0), Operator:=xlFilterCellColor
    End With
End Sub

The following result is obtained:

Resultado

Browser other questions tagged

You are not signed in. Login or sign up in order to post.