+ Reply to Thread
Results 1 to 2 of 2

Filter Sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    08-16-2011
    Location
    Tehran,Iran
    MS-Off Ver
    Excel 2007
    Posts
    25

    Filter Sheet

    Hi,

    I have some data in sheet 1 and I want to filter them using an userform.
    column A: Name
    column B: Num1
    column C: Num2
    in my user form I have a combobox to select Name, 2 textboxes to enter min and max for Num1, and 2 other textboxes to enter min and max for Num2.

    after I Enter the data the following code will be run by clicking on Filter Button

    Private Sub CommandButton1_Click()
        
        Dim EOR
        
        If TextBox2.Text <> Empty And TextBox1.Text > TextBox2.Text Then
            MsgBox "Wrong Numbers"
            Exit Sub
        End If
        If TextBox4.Text <> Empty And TextBox3.Text > TextBox4.Text Then
            MsgBox "Wrong Numbers"
            Exit Sub
        End If
        Sheet1.AutoFilterMode = False
        Sheet1.Range("A2:I1").AutoFilter
        
        EOR = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
    
        Sheet1.AutoFilterMode = False
        Sheet1.Range("A2:C" & EOR).AutoFilter
    
        With Sheet1.Range("A3:C" & EOR)
            
            If ComboBox1.Text <> Empty Then .AutoFilter Field:=1, Criteria1:="=" & ComboBox1.Text
            If TextBox1.Text <> Empty Then .AutoFilter Field:=2, Criteria1:=">=" & TextBox1.Value
            If TextBox2.Text <> Empty Then .AutoFilter Field:=2, Criteria1:="<=" & TextBox2.Value
            If TextBox3.Text <> Empty Then .AutoFilter Field:=3, Criteria1:=">=" & TextBox3.Value
            If TextBox4.Text <> Empty Then .AutoFilter Field:=3, Criteria1:="<=" & TextBox4.Value
        End With
    End Sub
    But it doesn't work correctly

    please help me

    the combobox and textboxes can be empty.

    if it is better to use Advanced filter, Please help me with that.

    see the file
    Attached Files Attached Files

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: Filter Sheet

    Delete all your code and replace with this code,
    Private Sub ComboBox1_Change()
        Dim s As String
        s = ComboBox1
        Range("A2").AutoFilter Field:=1, Criteria1:=s
    End Sub
    
    
    Private Sub TextBox2_Change()
        Dim Rws As Long, Rng As Range
        Rws = Cells(Rows.Count, "A").End(xlUp).Row
        Set Rng = Range(Cells(2, 1), Cells(Rws, 3))
        Rng.AutoFilter Field:=2, Criteria1:=">=" & TextBox1, _
                       Operator:=xlAnd, Criteria2:="<=" & TextBox2
    
    End Sub
    
    Private Sub TextBox4_Change()
        Dim Rws As Long, Rng As Range
        Rws = Cells(Rows.Count, "A").End(xlUp).Row
        Set Rng = Range(Cells(2, 1), Cells(Rws, 3))
        Rng.AutoFilter Field:=3, Criteria1:=">=" & TextBox3, _
                       Operator:=xlAnd, Criteria2:="<=" & TextBox4
    
    End Sub
    
    Private Sub UserForm_Initialize()
        ActiveSheet.AutoFilterMode = 0
        With ComboBox1
            .AddItem "a"
            .AddItem "b"
            .AddItem "c"
        End With
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 1
    Last Post: 12-12-2014, 06:46 AM
  2. Filter Several Sheets Based On Another Sheet Filter
    By VintuBr in forum Excel General
    Replies: 6
    Last Post: 12-06-2013, 10:40 AM
  3. Master Filter Sheet to Filter All Sheets
    By nguyeda in forum Excel Programming / VBA / Macros
    Replies: 29
    Last Post: 12-16-2012, 06:59 PM
  4. [SOLVED] Need: filter, paste info to new sheet, rename sheet, unfilter, hide column, filter next
    By esmithqg in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-18-2012, 04:49 PM
  5. [SOLVED] filter a sheet by column content, create new sheet with value in filtered cell
    By Footley in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-27-2012, 08:33 PM

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