Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 29

Thread: How to filter data with a multi-selection listbox

  1. #1
    VBAX Regular
    Joined
    Apr 2010
    Posts
    14
    Location

    Exclamation How to filter data with a multi-selection listbox

    Hello everyone,
    I'm new in VBA code and I've got a big problem
    There's a workbook with a sheet called "Database".
    I need to create a multi-selection listbox, in order to filter a particular column of the Database sheet.
    Any ideas??
    I created a very simple listbox, but I can't filter more than just 1 selection.
    Is the code similar to this one?
    [vba]Private Sub ListBox1_Change()

    Dim x As Variant, i As Variant, j As Variant
    Dim arr()
    x = ListBox1.ListCount - 1
    ReDim arr(x)
    For i = 0 To x
    If ListBox1.Selected(i) = True Then
    arr(j) = ListBox1.List(i)
    j = j + 1
    End If
    Next i
    ReDim Preserve arr(j - 1)
    On Error Resume Next
    For i = x + 1 To 1 Step -1
    SOMETHING.....
    Next

    End Sub[/vba]
    Thank you for your patience...

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You need to set up an Advanced Filter in the spreadsheet. In the criteria range, set the column heading for the criteria, and below that, populate the criteria fields. When you have the Advanced Filter working manually, then you can automate it.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular
    Joined
    Apr 2010
    Posts
    14
    Location
    ok!
    I'll try this solution... hope it works!

  4. #4
    VBAX Regular
    Joined
    Apr 2010
    Posts
    14
    Location
    Range("Database").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("Criteria")

    Uhm.... how can I specify the criteriarange in order to match the listbox selection???
    I'm not very good in VBA language...

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Create a sample file with the database and filter criteria in a suitable layout. Post it using Manage Attachments in the Go Advanced reply section and we'll go from there.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Regular
    Joined
    Apr 2010
    Posts
    14
    Location
    Here it is
    I want to create a Listbox with the items under the Criteria label.
    After that, i want to filter the database section based on what I choose in the Listbox (multiple selection allowed).
    Thank you in advance....

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Give this a try
    Last edited by mdmackillop; 04-22-2010 at 10:42 AM. Reason: File updated
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Regular
    Joined
    Apr 2010
    Posts
    14
    Location

    works great......

    thank you so much!
    Tomorrow I'll be able to test it more deeply..
    thanks a lot for now

  9. #9
    VBAX Regular
    Joined
    Apr 2010
    Posts
    14
    Location
    Just a question:
    is there a way to fill the listbox by opening the workbook instead of double-click on it??
    thx

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Adjusted
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    VBAX Regular
    Joined
    Apr 2010
    Posts
    14
    Location
    Ok!
    Works great!
    Thank you so much!

  12. #12
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    If the solution provided is to your satisfaction could you please mark the thread as solved by going to the Thread Tools dropdown ( just above your initial post) and selecting Thread solved please
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  13. #13
    VBAX Regular
    Joined
    Apr 2010
    Posts
    14
    Location
    Sorry,
    last question (I promise ):
    is there a way to have the listbox and the filter buttons on a different sheet?
    I need to make a Database on Sheet1 and the listbox on Sheet2...
    thx in advance

  14. #14
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You can. Try changing the references in the code. If you have problems I can look at this later.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  15. #15
    VBAX Regular
    Joined
    Apr 2010
    Posts
    14
    Location
    I changed every single reference in the workbook, but I'm stuck on the same line:
    Range("Database").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
    Range("CritRange"), Unique:=False

    For every change I make, I've always got a problem on this line of code.
    Can you help me out?
    Thank you

  16. #16
    VBAX Regular
    Joined
    Apr 2010
    Posts
    14
    Location
    Here is the file I tried to modify.
    I always get the same error, on the same line of the code...

  17. #17
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    My apologies. You cannot set a range reference to a different worksheet when you ate working from within a worksheet module ( I hope that makes sense)
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  18. #18
    VBAX Regular
    Joined
    Apr 2010
    Posts
    14
    Location
    There's no solution then?
    I really need to have the listbox on a different sheet

  19. #19
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    The code just need to be moved. See attachment in post #17
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  20. #20
    VBAX Regular
    Joined
    Apr 2010
    Posts
    14
    Location
    Ok sorry, I downloaded the wrong file.
    Problem solved.
    Thank you so much.
    You are awesome

Posting Permissions

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