Appling the next criteria in an AutoFilter

Hi,
I have a table with 2 columns. an filter has been set for the 2nd column. Now I need a script that applies "the next available filter" to column 1. Don't know how to explain - please see the attachment: what I need is a script that does the following: set the filter criteria for Column1 to "=4". And the other way - a script that sets the criteria for Column1 to "=1".

Thanks in advance for any ideas!

Regards Sebastian
example.xls
SebzapAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
You can simply use this code for 4 and if u want to do it to 1 then change 4 to 1...
Saurabh...

Sub filters()

    ActiveSheet.Range("$A$1:$B$18").AutoFilter Field:=1, Criteria1:="4"
End Sub

Open in new window

SebzapAuthor Commented:
Thanks for the comment, I know, but I want to iterate through the criterias in filter 1. The script should always go to the next available filter criteria.
Saurabh Singh TeotiaCommented:
So for that you need to create a loop statement like this first to store values and then to apply filter on stored values what i meant is this..
 

Sub filters()
    Dim rng As Range, cell As Range

    Dim lrow As Long, x As Variant, y As Variant, k As Variant

    lrow = 2
    Do Until lrow > Cells(Cells.Rows.Count, "A").End(xlUp).Row
        Set rng = Range("A2:A" & lrow)
        If Application.WorksheetFunction.CountIf(rng, Cells(lrow, "A").Value) = 1 Then
            If x = "" Then
                x = Cells(lrow, "A").Value
            Else
                x = x & "," & Cells(lrow, "A").Value
            End If
        End If
        lrow = lrow + 1
    Loop

    y = Split(x, ",")

    For Each k In y
        ActiveSheet.Range("$A$1:$B$18").AutoFilter Field:=1, Criteria1:=k
        'your code here

    Next k

End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

SebzapAuthor Commented:
Thanks so far. That gives me all the unique values in a column.
But what I need as the first step is more a list/an array of values that are shown when I click the button of a filter, e.g. have a look at the image --> I would need an array which contains c1, c2, c3, C4, c5, s2, s3, s4, S5, s6

Regards Sebastian

090245693083574.JPG
SebzapAuthor Commented:
SebzapAuthor Commented:
I managed to solve the problem - ugly, but works.

Thanks for helping!

Regards Sebastian


Private Sub CommandButton2_Click()
    NextFilter
End Sub

Private Sub CommandButton3_Click()
    PrevFilter
End Sub



Function maximum(arr)
    maxi = 0
    For Each wert In arr
        If wert <> "" Then
            If CInt(wert) > maxi Then
                
                maxi = wert
            End If
        End If
    Next wert
    maximum = maxi
    
End Function

Function aktFilter()
    If Not AutoFilter.Filters(1).On Then
        aktFilter = 0
        Exit Function
    End If
    aktFilter = Replace(AutoFilter.Filters(1).Criteria1, "=", "")
End Function

Function NextFilter()
    Application.ScreenUpdating = False
    werte = GetUniqueEntries(Range("A2:A65536"))
    akt = aktFilter()
    If akt = 0 Then
        ActiveSheet.Range("A2:A65536").AutoFilter Field:="1", Criteria1:=maximum(werte)
    Else
        activated = False
        For Each k In werte
            If activated Then
                ActiveSheet.Range("A2:A65536").AutoFilter Field:="1", Criteria1:=k
                If anzahlAktiveFilter() > 0 Then
                    Exit Function
                End If
            End If
            If k = akt Then
                activated = True
            End If
        Next k
    End If
End Function

Function PrevFilter()
    Application.ScreenUpdating = False
    werte = GetUniqueEntries(Range("A2:A65536"))
    akt = aktFilter()
    If akt = 0 Then
        ActiveSheet.Range("A2:A65536").AutoFilter Field:="1", Criteria1:=1
    Else
        activated = False
        For k = UBound(werte) To LBound(werte) Step -1
            If activated Then
                ActiveSheet.Range("A2:A65536").AutoFilter Field:="1", Criteria1:=werte(k)
                If anzahlAktiveFilter() > 0 Then
                    Exit Function
                End If
            End If
            If werte(k) = akt Then
                activated = True
            End If
        Next k
    End If
End Function

Function GetUniqueEntries(ByVal TheRange As Range) As String()
    Dim TempArr() As String, TempCt As Long, CLL As Range, i As Long
    Set TheRange = Intersect(TheRange, TheRange.Parent.UsedRange)
    TempCt = 0
    For Each CLL In TheRange.Cells
        For i = 0 To TempCt - 1
            If TempArr(i) = CLL.Text Then Exit For
        Next 'i
        If i = TempCt Then
            ReDim Preserve TempArr(TempCt)
            TempArr(TempCt) = CLL.Text
            TempCt = TempCt + 1
        End If
    Next 'CLL
    GetUniqueEntries = TempArr
End Function


Function anzahlAktiveFilter()
    Dim Bereich As Range
    Dim i As Integer
    
    Set Bereich = Range("A1").CurrentRegion
    anzahlAktiveFilter = Intersect(Bereich.SpecialCells(xlVisible), Bereich.Columns(1)).Count - 1
    
End Function

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.