+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Valued Forum Contributor Macdave_19's Avatar
    Join Date
    03-14-2007
    Location
    Nottingham, England
    MS-Off Ver
    12.0
    Posts
    762

    Question Listbox that alters a filter?

    Hi All,

    I'm trying to alter a filter ion a range to include all items selected within a listbox, here's what i have so far:

    Code:
    Dim Lb As ListBox
    Set Lb = ActiveSheet.ListBoxes(1)
        
    Range("TASKDATA").AutoFilter Field:=3
    
    With Lb
    Count = 0
        For i = 1 To .ListCount
            If .Selected(i) = True Then
                Range("TASKDATA").AutoFilter Field:=3, Criteria1:=Array(.List(i)), Operator:=xlFilterValues
            End If
        Next i
    End With
    it's only working when i select one of the items at a time? the items in the lists are just ID numbers at the moment.

    any help is greatly appreciated?

    thanks all
    Last edited by Macdave_19; 02-12-2010 at 10:50 AM.
    Mr MaGoo
    Magoo.Inc MMVII

    If i've helped please add to my Rep by Clicking on the Blue Scales in the top right hand corner of the post

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: Listbox that alters a filter?

    Try this,

    Code:
    Sub Macro2()
    '
    ' Macro2 Macro
    '
    Dim Lb As ListBox
    Dim vntItems() As Variant
    Dim Count As Long
    Dim i As Long
    
    Set Lb = ActiveSheet.ListBoxes(1)
        
    Range("TASKDATA").AutoFilter Field:=3
    
    With Lb
        Count = 0
        For i = 1 To .ListCount
            If .Selected(i) = True Then
                ReDim Preserve vntItems(Count) As Variant
                vntItems(Count) = CStr(i)
                Count = Count + 1
            End If
        Next i
    End With
    Range("TASKDATA").AutoFilter Field:=3, Criteria1:=vntItems, Operator:=xlFilterValues
    
    End Sub
    Cheers
    Andy
    www.andypope.info

  3. #3
    Valued Forum Contributor Macdave_19's Avatar
    Join Date
    03-14-2007
    Location
    Nottingham, England
    MS-Off Ver
    12.0
    Posts
    762

    Question Re: Listbox that alters a filter?

    Hi Andy,

    Cheers for attempting that it' not owking though?

    when i step into it and go through each part at the end it does nothing and the bit that reads

    vntItems says <subscript out of range>

    ??

    any idea's
    Mr MaGoo
    Magoo.Inc MMVII

    If i've helped please add to my Rep by Clicking on the Blue Scales in the top right hand corner of the post

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: Listbox that alters a filter?

    That would suggest nothing was selected in the listbox.

    Here's my example
    Attached Files Attached Files
    Cheers
    Andy
    www.andypope.info

  5. #5
    Valued Forum Contributor Macdave_19's Avatar
    Join Date
    03-14-2007
    Location
    Nottingham, England
    MS-Off Ver
    12.0
    Posts
    762

    Re: Listbox that alters a filter?

    Yo andy,

    i dunno what i'm doing wrong?

    could you have a butchers please:
    Attached Files Attached Files
    Mr MaGoo
    Magoo.Inc MMVII

    If i've helped please add to my Rep by Clicking on the Blue Scales in the top right hand corner of the post

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: Listbox that alters a filter?

    The index number is being stored in the array, which for my example was fine as I was using ids 1 to 10.

    This change will use the actual selected values.

    Code:
                vntItems(Count) = CStr(.List(i))
    Cheers
    Andy
    www.andypope.info

  7. #7
    Valued Forum Contributor Macdave_19's Avatar
    Join Date
    03-14-2007
    Location
    Nottingham, England
    MS-Off Ver
    12.0
    Posts
    762

    Thumbs up Re: Listbox that alters a filter?

    Cheers andy,

    i should have spotted that as that was what i was originally doing!

    thanks once again
    Mr MaGoo
    Magoo.Inc MMVII

    If i've helped please add to my Rep by Clicking on the Blue Scales in the top right hand corner of the post

Thread Information

Users Browsing this Thread

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

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.2.0