Autofilter data based on listbox selections

  • Greetings.


    Iam trying to auto filter a data range based on criteria passed from a list box selections.I want the auto filter to be filtering column 2 of the data range based on ALL the items SELECTED from a multi-select list box (named listbox2) at the click of a button. Here is what i currently have:


    It works, only that it filters the data by ONLY the last selected value of the list(i.e only one value item in the list). I want it to loop through ALL the selections, applying filter on column 2 based on EACH of the selections on the list box.


    Thanks in anticipation.

  • Re: Autofilter data based on listbox selections


    You cant cumulatively just add your filter cirteria to a filter. You can, at most have two criteria per "column" (field), with an AND/OR relationship in the filter. What you have above, is you have applied each selected listbox item in turn. Each item is applied to filter independantly/sperately what has gone before.


    To do what you want to do you need to create an array of the filter items (the selected items in the listbox) and then use the built up/constructed array as part of the filter criteria. This should get you started:



    Basically the array x stores your selected listbox items. The array x has size 0 at the start, and this has to grow by one to allow you store more listbox items.
    Ger

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: Autofilter data based on listbox selections


    THANKS ! That worked PERFECTLY !!I also appreciate your quick response, the explanation of how it works and why my code failed.


    I hope I wont be bothering you if i sweetened it further. How would it be if I had THREE multi-select Listboxes ?
    i.e I want to provide the user a form interface where he can select any values from three listboxes and filter the same data range
    selections in listbox1 will filter column 1, selections in listbox2 will filter column 2, and so on, up to listbox3. If selections are made on ALL the listboxes, then the data would represent values meeting ALL the selection criteria.


    I dont know much about Arrays but am sure there is a way to Loop through all the Listbox selections.


    Once again, thanks for the help.

  • Re: Autofilter data based on listbox selections


    Well, Ive given you the code for one listbox, so now is the time for you to start learning ;)


    Assuming each listbox refers to each column (and the content of each column / listbox is different), you would need an array per listbox.... and then filter each column for that respective array. So lets call the arrays x, y and z and lets call the listboxs - 1, 2 and 3 respectively.


    You now need three separate for loops to process each listbox (listbox1, listbox2, listbox3). In listbox1, you build up the array X using a loop like the code above, in listbox2, build up array Y using a loop and in listbox3 build up array Z using a Loop. The code is exactly the same as what you have above, just change the names of the listboxes and variables.


    When the 3 arrays are setup apply the arrays one at a time to each colulmn, again like the code above.


    Regards
    Ger

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: Autofilter data based on listbox selections


    Thanks, actually I had tried earlier before seeking your help, only that my code filters out the ENTIRE data range i.e all the data is invisible. This is so even if selections are made in only one listbox. I am relatively new to VBA (barely 5 months),and I am not very good with arrays, so I hoped for some kind of a loop to loop through all the listboxes.


    Anyway, this is what I had:



    Please direct me further. Thanks.

  • Re: Autofilter data based on listbox selections


    To be fair - at least you tried... and you were close enough in your attempt, and you admitted you didnt know arrays all that well, so I tightened it up the array declarations a bit and gave a full solution... see attached.


    Ger


    NB - please add code tags to your posts whenever you are posting code... these work like [noparse]

    Code
    'your code

    [/noparse] I added them for you - it makes code easier to read. ;)

    Files

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: Autofilter data based on listbox selections


    THANKS ! That worked like MAGIC !!!!


    That was more than I could have asked for.It blew me away, considering you even came up with an illustration.THANK YOU SIR !!


    OH! and by the way...there's a final listbox...yes there is.... where the data is in Months (Jan to Dec) over row range (G3:R3). I want the code to be able to hide the month column for unselected items, i.e hide all other columns if the user selected Jan to April.


    As always, I have given it a try, (based on the filter code for the other listboxes) but It doesn't do anything. Here's what I have...


    I hope to add this as the final piece to the previous code. Please direct me further. Many THANKS.

  • Re: Autofilter data based on listbox selections


    Sorry for the delay in replying - been kinda busy :) Hope this isnt too late...


    You dont need an array for this at all - just hide/unhide columns in the loop if the entry is selected or not - something like this:



    Actually... if the above code works... if it does what you are looking for, then this could be shortened to:

    Code
    Dim i As Integer
         
        Application.ScreenUpdating = False
             
        For i = 0 To ListBox4.ListCount - 1
            ActiveSheet.Columns(i + 7).Hidden = Not Me.ListBox4.Selected(i)
        Next i
         
        Application.ScreenUpdating = True


    Ger

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: Autofilter data based on listbox selections


    THANK YOU SIR !!!
    That was very helpful and I can call it a wrap.I have also learnt a lot and keen on improving further.


    Moreover...I hope you do not mind....:oops:


    I had another tougher issue, related to the previous one. It pertains to a similar project and am completely stuck. This time, due to the data format, It would require a Treeview control.


    The data format is as:


    Beverages - Tea
    - Coffee
    - Lemonade
    - Soda


    Fruits - Mango
    - Apple
    - Orange


    e.t.c


    the categories are in column A while the items are in column B. I am not familiar with Treeview properties and events as Ive never used it. I want code to populate the data to the treeview from the two columns, with Column A values as the Parent and its corresponding values in Column B as the children. I believe it is possible using Arrays, or any other way, and thereafter apply the treeview selections as filters to the data range at the click of a button. I hope I'm clear.


    I look forward to your guidance. Thank you for your valuable help thus far.


    Actuarish

  • Re: Autofilter data based on listbox selections


    Hi Actuarish... well, i think even this time, I am stuck :)


    Never used the treeview control to be honest, but what you request seems reasonable in terms of populating the values of the treeview.


    Start a new thread with a title of "Populate Treeview from Column values"....


    Also, dont forget to take a look at the possible answers section under your post.


    Also - this might help you (check them before starting a new thread):
    http://www.xtremevbtalk.com/showthread.php?t=311338
    http://www.excelguru.ca/conten…-Using-a-Treeview-Control


    Regards
    Ger

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: Autofilter data based on listbox selections


    Thank you for the links. I basically copy-pasted the code but it did not work the way I hoped. I will just post my issue in the forums and hope for the best. Thank you. Actuarish

  • Re: Autofilter data based on listbox selections


    actuarish,


    I seem to be having the same problem that you had with capturing an array to get it to filter. I used the code you all had but it still only takes the first value selected. From you workings with this did you ever have any problems preserving the first value and adding an additional to the array?


    Any help would be appreciate thanks a head of time

  • Re: Autofilter data based on listbox selections


    Welcome to OzGrid, HillBilly12.


    Please do not post questions in threads started by other members. Start your own thread, give it an accurate and concise title that briefly describes your issue and explain your problem fully.


    If you think this threads can help clarify your issue you can include a link by copying the URL from the address bar of your browser and pasting into your message.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!