'Excel VBA: Autofilter using textbox values

I tried the following to filter (between filter) my worksheet based on 2 values. The 2 values are input in textbx1 and textbox2 in userform 'A1_Filter_Range'.

But the code doesn't work ... I get error: Run-Time error'424': Object required'

    ActiveSheet.Range("$A$1:$AD$2000").AutoFilter Field:=17, _
    Criteria1:=TextBox1.Value, _
        Operator:=xlAnd, _
    Criteria2:=TextBox2.Value

Any ideas? Thanks SMORF



Solution 1:[1]

Simple solution, you have to tell Excel where to look for the TextBoxes:

ActiveSheet.Range("$A$1:$AD$2000").AutoFilter Field:=17, _
    Criteria1:=ActiveSheet.TextBox1.Value, _
    Operator:=xlAnd, _
    Criteria2:=ActiveSheet.TextBox2.Value

Solution 2:[2]

If TextBox1.Text <> "" Then

        TextBox1.BackColor = RGB(254, 254, 22) 'yellow

        Dim word As String


        word = "*" & TextBox1.Text & "*"
        Selection.AutoFilter Field:=1, Criteria1:=word, Operator:=xlAnd

    Else
        Selection.AutoFilter Field:=1
        TextBox1.BackColor = RGB(55, 255, 255) 'cyan

    End If
            'ActiveSheet.Protect Password:=""

End Sub

Private Sub TextBox2_Change()
                'ActiveSheet.Unprotect Password:=""

 If TextBox2.Text <> "" Then

        TextBox2.BackColor = RGB(254, 254, 22) 'yellow

        Dim slovo As String


        word = "*" & TextBox2.Text & "*"
        Selection.AutoFilter Field:=2, Criteria1:=word, Operator:=xlAnd

    Else
        Selection.AutoFilter Field:=2
        TextBox2.BackColor = RGB(55, 255, 255) 'cyan

    End If
                'ActiveSheet.Protect Password:=""
End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Physikbuddha
Solution 2 Vega