0

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

1
  • Your code is having a hard time finding the parent userform for the textboxes. A solution will depend where the above snippet resides. Commented May 19, 2015 at 19:07

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

If the TextBoxes are placed on another worksheet than the current one, replace ActiveSheet. with Worksheets(#). where # is the worksheet index.
1
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

1 Comment

Can you explain to the author what is wrong with his code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.