Results 1 to 2 of 2

Thread: [RESOLVED] Use Textbox.Value in AutoFilter Criteria

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    Resolved [RESOLVED] Use Textbox.Value in AutoFilter Criteria

    OK, I have the following code generated by autofiltering on a list of names using a text filter "Contains" and then the typed text, "Juan":

    Code:
    '    ActiveSheet.Range("$A$2:$P$611").AutoFilter Field:=4, Criteria1:="=*Juan*" _
    '        , Operator:=xlAnd
    I would like a search button in my UserForm to activate this code when clicked. The difference is, in place of "Juan" I would like to have whatever the user types in TextBox14, which would be in the same UserForm as the search button. The following was my attempt at the search button's code:

    Code:
    Private Sub CommandButton26_Click()
        ActiveSheet.Range("$A$2:$P$611").AutoFilter Field:=4, Criteria1:="=" & UserForm1.TextBox14.Value & "*" _
            , Operator:=xlAnd
    End Sub
    When clicked, I get no errors, and it filters my data, but it doesn't work correctly; all of the rows get filtered out, which must mean that whatever my code's autofilter criteria resolves to, it's not in the column of names. Any ideas on how to write this criteria so that my textbox's value will get used in the filter?

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    Re: Use Textbox.Value in AutoFilter Criteria

    I figured it out, if anyone is interested:

    Code:
    Private Sub CommandButton26_Click()
        ActiveSheet.Range("$A$2:$P$611").AutoFilter Field:=4, Criteria1:="=" & "*" & UserForm1.TextBox14.Value & "*" _
            , Operator:=xlAnd
    End Sub

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width