Consulting

Results 1 to 8 of 8

Thread: Sleeper: VBA Filtering

  1. #1
    VBAX Regular
    Joined
    Jul 2004
    Location
    San Bernardino, California
    Posts
    69
    Location

    Sleeper: VBA Filtering

    I'm not an Excel VBA Person by any means, I can get by only when necessary.

    Well this time I need to get by and I'm too damn tired to fight with this thing.

    How do you filter a sheet based on a selection in a combo box?

    I've got the combo box made, it's populating appropriately.

    Now I just need to filter a specific column for all of the data from Row F6 and down.

    Just a quick sample will help and I can get anything else I need. I just don't use Excel enough to know where to start with filtering.

    - Anthony

  2. #2
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi Anthony,

    It depends on what type of combo box you've created...The simplest one to answer is - If it's one made using the "Forms" toolbar then right-click the combo-box, select Format Control, and select "Control" then in the "Input Range" put $F6:$F100 (you can put the second input range to whatever you like 100, 1000, ...,down to 65536).

    If it's a "Control" toolbar you'll need code for it. (Let me know)

    As you're mentioning filtering, you could also look at Tools/Filter/AutoFilter

    Regards,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  3. #3
    VBAX Regular
    Joined
    Jul 2004
    Location
    San Bernardino, California
    Posts
    69
    Location
    OK, Well here is what I found after searching. I seen alot of people use AutoFilter, so I attempted to use it, but I'm definitely missing something.

    If Me.MyComboBox.Value = "X" Then
    'No Filter, show all data
            Range("NewData").AutoFilter 6, "*", , , False
    Else
    'Filter 6th column based on combo box value
            Range("NewData").AutoFilter 6, Me.MyComboBox.Value, , , False
    End If
    Any Ideas?

    - Anthony

  4. #4
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    You might find this easier, select F6, now go to Tools, Filter, and select Autofilter. You'll now see a combo-box drop-down on the RHS of F6.

    Select the drop-down, then scroll down and select what you're looking for. Everything else should now be filtered out.

    You can also select "Custom" and type in what you're looking for in the top RH panel.

    See if that does what you want
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  5. #5
    VBAX Regular
    Joined
    Jul 2004
    Location
    San Bernardino, California
    Posts
    69
    Location
    Quote Originally Posted by johnske
    You might find this easier, select F6, now go to Tools, Filter, and select Autofilter. You'll now see a combo-box drop-down on the RHS of F6.

    Select the drop-down, then scroll down and select what you're looking for. Everything else should now be filtered out.

    You can also select "Custom" and type in what you're looking for in the top RH panel.

    See if that does what you want
    I already know how to do it that way, I need the combo box to be in another location which is why I'm doing it the way I am trying.

    Any ideas?

    - Anthony

    OK, I think I'm on the right track now.

    I'm using .AdvancedFilter

    However, the problem I'm having is telling the code which column to filter on.

    Here's what I have so far:


    If Me.cboX.Value = "X" Then
    Range("Criteria").Value = "*"
            Range("NewData").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("Criteria"), Unique:=False
    Else
    Range("Criteria").Value = Me.cboX.Value
            Range("NewData").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("Criteria"), Unique:=False
    End If


    Any Ideas?

    - Anthony

  6. #6
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi Anthony,

    Try this, you'll need to have your combo box named the same as the one in the code below (ComboBox1):

    Option Explicit
     
    Private Sub ComboBox1_DropButtonClick()
    'populate the combo box
    Dim MyList(200, 1), N&
    For N = 6 To 200
    MyList(N - 6, 0) = Range("F" & N)
    Next N
    ComboBox1.List = MyList
    End Sub
     
    Private Sub ComboBox1_Change()
    With Range("F1:F200")
    If ComboBox1 = "X" Then
    'No Filter, show all data
    .AutoFilter 1, "*", , , False
    Else
    'Filter 6th column based on combo box value
    .AutoFilter 1, ComboBox1, , , False
    End If
    End With
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  7. #7
    VBAX Regular
    Joined
    Jul 2004
    Location
    San Bernardino, California
    Posts
    69
    Location
    With that I'm getting AutoFilter Method of Range Class Failed.

    Any Ideas on the AdvancedFilter Method I was trying?

    - Anthony

  8. #8
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by ALaRiva
    With that I'm getting AutoFilter Method of Range Class Failed.

    Any Ideas on the AdvancedFilter Method I was trying?

    - Anthony
    Don't know why you're getting an error - try the attachment, I've been using this with no probs...

    As to AdvancedFilter - No - I've never used it so can't say.

    Regards,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

Posting Permissions

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