+ Reply to Thread
Results 1 to 4 of 4

userform one combobox dependent on another

Hybrid View

  1. #1
    Registered User
    Join Date
    07-05-2010
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    7

    userform one combobox dependent on another

    I have a userform that includes 2 combo boxes (cboRoadway, cboStreet). These 2 are dependent on each other. It is comparable to a city / state drop down list.
    By selecting roadway AA, you would get a list of matching cross streets for AA.
    By selecting roadway BB, you would get a new list of matching cross streets for BB.

    cboRoadway list values are populated in my userform_initialize statement.

    Scenario: user selects a roadway, then selects a matching street. The user realizes they chose the wrong roadway, so they go back and change the roadway.
    I need the street list to clear and refresh so that they new street choices match the new roadway selection.

    I hope this makes sense. Go easy on me since I've only done a handful of my own VBA projects.


    Private Sub cboRoadway_AfterUpdate()
        Dim cStreeet As Range
        Dim ws As Worksheet
        Set ws = Worksheets("Data")
        Select Case cboRoadway.Value
            Case "AA"
                For Each cStreet In ws.Range("e1:e19")
                With Me.cboStreet
                    .AddItem cStreet.Value
                    .List(.ListCount - 1, 1) = cStreet.Offset(0, 1).Value
                End With
                Next cStreet
            Case "BB"
                For Each cStreet In ws.Range("f1:f15")
                With Me.cboStreet
                    .AddItem cStreet.Value
                    .List(.ListCount - 1, 1) = cStreet.Offset(0, 1).Value
                End With
                Next cStreet
            Case "CC"
                For Each cStreet In ws.Range("g1:g18")
                With Me.cboStreet
                    .AddItem cStreet.Value
                    .List(.ListCount - 1, 1) = cStreet.Offset(0, 1).Value
            
                End With
                Next cStreet
            End Select
        
    End Sub

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: userform one combobox dependent on another

    Do not use additem to populate a combobox or listbox

    This will be sufficient:
    Private Sub cboRoadway_AfterUpdate()
        cbostreet.list = sheets("Data").cells(1,1).offset(,cborailway.listindex+1).specialcells(2).value
    End Sub
    I suppose
    roadways in column A
    streets of first roadway in column B
    streets of second roadway in column C, etc.
    Last edited by snb; 07-06-2010 at 03:57 AM.

  3. #3
    Registered User
    Join Date
    07-05-2010
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: userform one combobox dependent on another

    Quote Originally Posted by snb View Post
    Do not use additem to populate a combobox or listbox

    This will be sufficient:
    Private Sub cboRoadway_AfterUpdate()
        cbostreet.list = sheets("Data").cells(1,1).offset(,cborailway.listindex+1).specialcells(2).value
    End Sub
    I suppose
    roadways in column A
    streets of first roadway in column B
    streets of second roadway in column C, etc.
    Is there additional code I need with the above replacement? I replaced my code in cboRoadway_afterupdate with the above code. It returns the values for cboRoadway. Is there additional code I need somewhere else to make the above line work?

    Column1 - contains list of major roadways (cboRoadway) such as IH-35E, IH-635E, US-75
    Column2 - contains list of streets (cboStreet) that cross only IH-35E
    Column3 - contains list of streets (cboStreet) that cross only IH-635E
    Column4 - contains list of streets (cboStreet) that cross only US-75

    When the user selects US-75 in cboRoadway, I want only the list in Column4 to appear in the cboStreets list.

  4. #4
    Forum Contributor Gabor's Avatar
    Join Date
    02-15-2004
    Location
    Székesfehérvár, Hungary
    MS-Off Ver
    xl2016
    Posts
    226

    Re: userform one combobox dependent on another

    At initialization of the userform you get the comboboxes filled, except the cboStreet

    Private Sub Userform_Initialize()
    'your code
    FillCBs
    'your code
    End Sub
    The following fills in your comboboxes:

    Private Sub FillCBs()
    With cboRoadway
       .AddItem ("RW1")
       .AddItem ("RW2")
       .AddItem ("RW3")
       .AddItem ("RW4")
    End With
    'and so on
    End Sub
    The following runs when user selects any out of the dropdown:

    Private Sub cboRoadway_Change(): FillcboStreet: End Sub
    
    Sub FillcboStreet()
    Select Case cboRoadway
    Case "RW1"
        cboStreet.Clear
        With cboStreet
            .AddItem ("S11")
            .AddItem ("S12")
            .AddItem ("S13")
            .AddItem ("S14")
        End With
    Case "RW2"
        cboStreet.Clear
        With cboStreet
            .AddItem ("S21")
            .AddItem ("S22")
            .AddItem ("S23")
            .AddItem ("S24")
        End With
    Case "RW3"
        cboStreet.Clear
        With cboStreet
            .AddItem ("S31")
            .AddItem ("S32")
            .AddItem ("S33")
        End With
    ' and so on
    Case Else
        cboStreet.Clear
    End Select
    End Sub
    If I'm not mistaken about the request.
    Not tested.
    Regards,

    Gabor

    Protect trees.. maybe one day we need to climb back....

+ 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