+ Reply to Thread
Results 1 to 3 of 3

Populate Listbox2 based on Listbox1 selection

  1. #1
    Forum Contributor
    Join Date
    06-11-2009
    Location
    Cape Town
    MS-Off Ver
    Microsoft 365
    Posts
    854

    Populate Listbox2 based on Listbox1 selection

    I would like to populate the 2nd List box based on the selection of the 1st List box? The product codes are unique to a supplier in my workbook. I have attached a sample file.
    Thanks in advance
    Attached Files Attached Files

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Populate Listbox2 based on Listbox1 selection

    Aland2929,

    Attached is a modified version of your test file.
    I have provided two different solutions, a Formula solution and the requested VBA userform solution.

    For the formula solution:
    First I converted the data in columns A:D into a table (Selected the data -> Insert tab -> Table)
    Then in cell F2 and copied down is this formula to get the list of unique suppliers:
    Please Login or Register  to view this content.
    Next I created a dynamic named range called listUnqSuppliers with this named range formula:
    Please Login or Register  to view this content.
    Cell J1 uses a data validation list with that named range to display a drop-down list of the unique suppliers.
    To get the list of the products for the chosen supplier, in cell G2 and copied down is this formula:
    Please Login or Register  to view this content.
    I also created a dynamic named range called listSupplierProducts with this named range formula:
    Please Login or Register  to view this content.
    Cell J2 uses a data validation list with that named range to display a drop-down list of the products. The list only contains products for the selected Supplier. I threw in a couple of extra formulas in cells J4 and J5 to display the Code and Units for the chosen Supplier and Product.



    On to the VBA portion. First I deleted the RowSource property of ListBox1, and I used the Userform_Initialize event in order to populate ListBox1:
    Please Login or Register  to view this content.

    Next I used the ListBox1_Change event in order to populate ListBox2:
    Please Login or Register  to view this content.

    Hopefully you can adapt one of these solutions to fit your needs.
    Attached Files Attached Files
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Forum Contributor
    Join Date
    06-11-2009
    Location
    Cape Town
    MS-Off Ver
    Microsoft 365
    Posts
    854

    Re: Populate Listbox2 based on Listbox1 selection

    Absolutely brilliant! Thanks so much for your time and effort. These solutions are perfect.

+ Reply to Thread

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.6.0 RC 1