+ Reply to Thread
Results 1 to 9 of 9

Userform Listbox Conditional on Previous Selection

Hybrid View

  1. #1
    Registered User
    Join Date
    05-23-2011
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    79

    Userform Listbox Conditional on Previous Selection

    Hello,

    I have to populate a list box using the conditional value selected in a previous listbox. That value selected in the first list box corresponds to its own range of values listed in the excel sheet. What is the code that will populate the second list box with the range corresponding to the value of the first?

    Attached is the sheet.

    for example, When Cambridge is selected in the first Listbox, I would like another listbox to open with the range below "Cambridge MC" on the combolists tab.

    Thanks
    Attached Files Attached Files

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Userform Listbox Conditional on Previous Selection

    Try replacing your code with this & see if it is what you want
    Private Sub cmdOK_Click()
        Dim ws As Worksheet
        Set ws = Worksheets("Output")
        ws.Cells(1, 1).Value = Me.listMNGR.Value
        Unload Me
    End Sub
    
    Private Sub listMNGR_Click()
        Dim lCol As Long
        lCol = Me.listMNGR.ListIndex + 4
        With ThisWorkbook.Sheets("combolists")
            output1.listMC.List = .Range(.Cells(2, lCol), .Cells(2, lCol).End(xlDown)).Value
        End With
        Me.Hide
        output1.Show
    End Sub
    
    
    Private Sub UserForm_Initialize()
    
        Dim cell As Range
        With ThisWorkbook.Sheets("combolists")
            listMNGR.List = .Range(.Cells(2, 1), .Cells(2, 1).End(xlDown)).Value
        End With
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    05-23-2011
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    79

    Exclamation Re: Userform Listbox Conditional on Previous Selection

    Thanks but the code does not populate the second list box (blank)

    Its not exactly what i need to do. So first, I would like the trigger to remain on the cmdOK button, instead of the listMNGR_Click.

    Also, I would like it to pull the corresponding list for the second list box depending on the first list box value chosen. I have updated the example sheet with more lists to choose from. I am assuming this will require some sort of indirect/dynamic syntax to reference the correct column, as it will change according the the selection in the first box. For the time being, I would like to retrieve the "MC" list for each name, not the "CG"
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    05-23-2011
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    79

    Re: Userform Listbox Conditional on Previous Selection

    in other words, is there a way to have the list box populate based on a search-type function of the excel sheet. So that the code will search for a header with the same name as what was selected in the first box, and retrieve the cells below it as the box fill??

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Userform Listbox Conditional on Previous Selection

    It does populate the second listbox. Your explanation was confusing so I set it up to load what I took to be the required column.

    Your manager selections do not match the other column headings

    This does what I think you need. It will be much faster than a search
    Attached Files Attached Files
    Last edited by royUK; 06-07-2011 at 09:38 AM.

  6. #6
    Registered User
    Join Date
    05-23-2011
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    79

    Re: Userform Listbox Conditional on Previous Selection

    is there a way to have this feed off a search for the header? I still cannot get it to populate correctly

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Userform Listbox Conditional on Previous Selection

    See my previous post that I've edited

  8. #8
    Registered User
    Join Date
    05-23-2011
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    79

    Re: Userform Listbox Conditional on Previous Selection

    thank you!

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Userform Listbox Conditional on Previous Selection

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

+ 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