+ Reply to Thread
Results 1 to 2 of 2

Using Textbox to define autofilter criteria

Hybrid View

  1. #1
    Registered User
    Join Date
    01-12-2012
    Location
    Dublin
    MS-Off Ver
    Excel 2010
    Posts
    50

    Using Textbox to define autofilter criteria

    Hi,
    I really appreciate any help you can give me with regards to this issue.

    I am attempting to use criteria, entered by the user, through a textbox I have implemented in a userform as the basis of the criteria for an autofilter. The code I have so far is below;

    Private Sub CancelButton_Click()
    
    Private Sub TextBox1_Change()
    x = TextBox1.Value
    End Sub
    
    Private Sub OKButton_Click()
    Application.ScreenUpdating = False
    If OptionHSDPA_SR Then
    
        Range("E3").Select
        Selection.AutoFilter
        Selection.AutoFilter Field:=5, Criteria1:="& x" , Operator:=xlAnd
    '' The value I want to enter into the textbox will be in the form of, =100, <=99, >90 and so on, and I want this to be the criteria for the autofilter. Although an error does not occur the code does not work effectively and the filter does not to do what I want it to do.

    ''However, if I define x = "<=95" and change the line of code too, Selection.AutoFilter Field:=5, Criteria1:=x , Operator:=xlAnd, it works but I need to specify the operand in the textbox.

    End If

    Any help would be great.
    Regards,
    David
    Last edited by kinseld5; 02-06-2012 at 07:32 AM.

  2. #2
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: Using Textbox to define autofilter criteria

    welcome on the forum - first of all please read forum rules and use code tags

    You wrote
    Selection.AutoFilter Field:=5, Criteria1:="& x" , Operator:=xlAnd
    so I think that excel expect second Criteria

    Let say you have Autofilter in range("E3")
    so you can organize data using following code
    Private Sub OKButton_Click()
    Application.ScreenUpdating = False
    If OptionHSDPA_SR Then
    
    ActiveSheet.Range("E3").AutoFilter Field:=1, Criteria1:=textbox1.text
    'or
    ' ActiveSheet.Range("E3").AutoFilter Field:=1, Criteria1:=textbox1.text, Operator:=xlAnd, Criteria2:=textbox2.text  'for use two criteria
    
    Application.ScreenUpdating = False
    End if
    End Sub
    Best Regards
    MaczaQ
    ---------------------------------------------------------------------------------------------------------------------------
    If you are satisfied with the solution(s) provided, please mark your thread as Solved
    If you are pleased with my answer consider to rate it. To thank someone who has helped you, click on the star icon below their name.
    - This way you will add him some reputation points ... thanks in advance.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1