+ Reply to Thread
Results 1 to 9 of 9

Repeating dependent ComboBoxes in a Userform

Hybrid View

  1. #1
    Registered User
    Join Date
    01-26-2016
    Location
    Houston, Texas
    MS-Off Ver
    2010
    Posts
    4

    Repeating dependent ComboBoxes in a Userform

    Hello,

    I have a pair of dependent ComboBoxes in a user form. ComboBox1 and ComboBox2. ComboBox2 is dependent on ComboBox1.

    This pair appears several times in the user form in the form of (ComboBox3 and ComboBox4), (ComboBox5 and ComboBox6), ……..etc. next to a text box which contains a description of machinery condition. The user has to use the ComboBoxes only in the case of a failure to the piece of machinery.

    I am currently using in the attachment.

    This code is sufficient only if there is a single pair of dependent ComboBoxes. Is there way to extend this code for all ComboBoxes such that it is called only when a pair of ComboBoxes is used.

    Thank you in advance.

    ef1.txt

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Repeating dependent ComboBoxes in a Userform

    You are going to need to code each combobox pair in a similar fashion as what you submitted.
    If you are happy with my response please click the * in the lower left of my post.

  3. #3
    Registered User
    Join Date
    01-26-2016
    Location
    Houston, Texas
    MS-Off Ver
    2010
    Posts
    4

    Re: Repeating dependent ComboBoxes in a Userform

    Hi stnkynts. Thanks for your reply.

    The data in all the pairs is identical. Do I still need to do this? Sorry, I am new to VBA.

    Thanks!

  4. #4
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Repeating dependent ComboBoxes in a Userform

    You should be able to just copy and paste the code from the ComboBox1_Change event to the CombboBox3_Change event and the ComboBox5_Change event. You will then need to change the associated combobox that affects it (i.e. ComboBox2 to 4, etc).

    You will also want to amend the UserForm_Initialize event so that it properly populated your combobox1, 3, and 5 (see below code)
    Private Sub UserForm_Initialize()
    
    With ComboBox1
        .AddItem "PartA"
        .AddItem "PartB"
        .AddItem "PartC"
    End With
    
    With ComboBox3
        .AddItem "PartA"
        .AddItem "PartB"
        .AddItem "PartC"
    End With
    
    With ComboBox5
        .AddItem "PartA"
        .AddItem "PartB"
        .AddItem "PartC"
    End With
    
    End Sub
    Example:
    Private Sub ComboBox3_Change()
    
    Dim index As Integer
    index = ComboBox1.ListIndex
    
    ComboBox4.Clear
    
    Select Case index
        Case Is = 0
            With ComboBox4
                .AddItem "FMA_1"
                .AddItem "FMA_2"
                .AddItem "FMA_3"
            End With
        Case Is = 1
            With ComboBox4
                .AddItem "FMB_1"
                .AddItem "FMB_2"
                .AddItem "FMB_3"
            End With
        Case Is = 2
            With ComboBox4
                .AddItem "FMC_1"
                .AddItem "FMC_2"
                .AddItem "FMC_3"
            End With
    End Select
    
    End Sub

  5. #5
    Registered User
    Join Date
    01-26-2016
    Location
    Houston, Texas
    MS-Off Ver
    2010
    Posts
    4

    Re: Repeating dependent ComboBoxes in a Userform

    Yes, I realize that but there are atleast 50 pairs of dependent comboboxes. I guess I was just hoping that there was a way to add a module that could be called in each Combobox_Change() function.

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,357

    Re: Repeating dependent ComboBoxes in a Userform

    The Initialize part can be shortened like this.
    Private Sub UserForm_Initialize()
        For i = 1 To 5 Step 2
            Me("ComboBox" & i).List = Array("PartA", "PartB", "PartC")
        Next
    End Sub
    The rest will take a look later.

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Repeating dependent ComboBoxes in a Userform

    Over fifty pairs, that calls for a Class Module
    Add a Class Module. Use the Properties Window to change its name to clsPairedDependents. Then put this code into the class's code module.
    ' in clsPairedDependents
    
    Public WithEvents MasterBox As MSForms.ComboBox
    Public WithEvents DependentBox As MSForms.ComboBox
    
    Dim DependentLists() As Variant
    Dim DisableMyEvents As Boolean
    Event Change()
    Event MasterChange()
    Event DependentChange()
    
    Sub AddDependentList(Index, ParamArray ListItems() As Variant)
        Dim ListOfItems As Variant
        If UBound(DependentLists) < Index Then ReDim Preserve DependentLists(0 To Index)
        If TypeName(ListItems(0)) Like "*()" Then
            ListOfItems = ListItems(0)
        Else
            ListOfItems = ListItems
        End If
        DependentLists(Index) = ListOfItems
    End Sub
    
    Public Sub AddDependentItem(strItem As String, Optional Index As Long = -1)
        With DependentBox
            If Index < 0 Then
                .AddItem strItem
            Else
                .AddItem strItem, Index
            End If
        End With
    End Sub
    
    Public Sub AddMasterItem(strItem As String, Optional Index As Long = -1)
        With MasterBox
            If Index < 0 Then
                .AddItem strItem
            Else
                .AddItem strItem, Index
            End If
        End With
    End Sub
    
    Property Get DependentList(Optional rIndex As Long = -1, Optional cIndex As Long = -1) As Variant
        If cIndex = -1 And rIndex = -1 Then
            DependentList = DependentBox.List
        Else
            If cIndex = -1 Then cIndex = 0
            List = DependentBox.List(rIndex, cIndex)
        End If
    End Property
    Property Let DependentList(Optional rIndex As Long = -1, Optional cIndex As Long = -1, inVal As Variant)
        If cIndex = -1 And rIndex = -1 Then
            DependentBox.List = inVal
        Else
            If cIndex = -1 Then cIndex = 0
            DependentBox.List(rIndex, cIndex) = inVal
        End If
    End Property
    
    Property Get DependentListCount() As Long
        DependentListCount = DependentBox.ListCount
    End Property
    
    Property Get DependentListIndex() As Long
        DependentListIndex = DependentBox.ListIndex
    End Property
    Property Let DependentListIndex(inVal As Long)
        DependentBox.ListIndex = inVal
    End Property
    
    Property Get DependentText() As String
        DependentText = DependentBox.Text
    End Property
    Property Let DependentText(inVal As String)
        DependentBox.Text = inVal
    End Property
    
    Property Get DependentValue() As Variant
        DependentValue = DependentBox.Value
    End Property
    Property Let DependentValue(inVal As Variant)
        DependentBox.Value = inVal
    End Property
    
    Property Get MasterList(Optional rIndex As Long = -1, Optional cIndex As Long = -1) As Variant
        If cIndex = -1 And rIndex = -1 Then
            MasterList = MasterBox.List
        Else
            If cIndex = -1 Then cIndex = 0
            List = MasterBox.List(rIndex, cIndex)
        End If
    End Property
    Property Let MasterList(Optional rIndex As Long = -1, Optional cIndex As Long = -1, inVal As Variant)
        If cIndex = -1 And rIndex = -1 Then
            MasterBox.List = inVal
        Else
            If cIndex = -1 Then cIndex = 0
            MasterBox.List(rIndex, cIndex) = inVal
        End If
    End Property
    
    Property Get MasterListCount() As Long
        MasterListCount = MasterBox.ListCount
    End Property
    
    Property Get MasterListIndex() As Long
        MasterListIndex = MasterBox.ListIndex
    End Property
    Property Let MasterListIndex(inVal As Long)
        MasterBox.ListIndex = inVal
    End Property
    
    Property Get MasterText() As String
        MasterText = MasterBox.Text
    End Property
    Property Let MasterText(inVal As String)
        MasterBox.Text = inVal
    End Property
    Property Get MasterValue() As Variant
        MasterValue = MasterBox.Value
    End Property
    Property Let MasterValue(inVal As Variant)
        MasterBox.Value = inVal
    End Property
    
    Property Get Name() As String
        Name = "PairedCombo_" & MasterBox.Name & "_" & DependentBox.Name
    End Property
    Property Get UFParent() As Object
        Set UFParent = MasterBox
        On Error Resume Next
        Do
            Set UFParent = UFParent.Parent
        Loop Until Err
        On Error GoTo 0
    End Property
    
    Private Sub Class_Initialize()
        ReDim DependentLists(0 To 0)
    End Sub
    
    Private Sub Class_Terminate()
        Set DependentBox = Nothing
        Set MasterBox = Nothing
    End Sub
    
    Private Sub DependentBox_Change()
        If DisableMyEvents Then Exit Sub
        Set UFParent.ActiveDependentPair = Me
        
        RaiseEvent Change
        RaiseEvent DependentChange
    End Sub
    
    Private Sub MasterBox_Change()
        If DisableMyEvents Then Exit Sub
        DisableMyEvents = True
        DependentBox.Text = ""
        DisableMyEvents = False
        If MasterBox.ListIndex <> -1 Then
            If UBound(DependentLists) < MasterBox.ListIndex Then
                DependentBox.Clear
            Else
                If TypeName(DependentLists(MasterBox.ListIndex)) Like "*()" Then
                    DependentBox.List = DependentLists(MasterBox.ListIndex)
                Else
                    DependentBox.Clear
                End If
            End If
        End If
        Set UFParent.ActiveDependentPair = Me
        
        RaiseEvent Change
        RaiseEvent MasterChange
    End Sub
    In this Demo user form, I used only 6 Comboboxes and made ComboBox1 the Master box for Combobox2, Combobox3 masters ComboBox4, etc.
    The loop in the Initialize event will have to be modified to match the pairings that you want.
    Notice that changing either of the paired boxes will 1) make that pair the ActiveDependentPair and fire the ActiveDependentPair_Change event.
    In this demo form, the ActiveDependentPair_Change event sets the .Caption of a Label to show some of the properties of clsDependentPair. (The Object Browser will show all of those properties, they mirror the properties of a ComboBox)

    Depending on if the Master or the Dependent of the pair caused the Change, either the ActiveDependentPair_MasterChange ActiveDependentPair_DependentChange will fire after the _Change event (but never both)

    ' in userform's code module
    
    Dim DependentPairs As Collection
    Public WithEvents ActiveDependentPair As clsPairedDependents
    
    Private Sub UserForm_Initialize()
        Dim MasterList As Variant
        Dim DependentLists(0 To 2)
        Dim newPair As clsPairedDependents
        Dim i As Long
       
        DependentLists(0) = Array("FMA_1", "FMA_2", "FMA_3")
        DependentLists(1) = Array("FMB_1", "FMB_2", "FMB_3")
        DependentLists(2) = Array("FMC_1", "FMC_2", "FMC_3")
        MasterList = Array("PartA", "PartB", "PartC")
        
        Set DependentPairs = New Collection
        
        Rem loop and match master to dependent comboboxes
        For i = 1 To 5 Step 2
            Set newPair = New clsPairedDependents
            
            With newPair
                Set .MasterBox = Me.Controls("ComboBox" & i)
                Set .DependentBox = Me.Controls("ComboBox" & (i + 1))
                
                .AddDependentList 0, DependentLists(0)
                .AddDependentList 1, DependentLists(1)
                .AddDependentList 2, DependentLists(2)
                .MasterList = MasterList
            End With
            
            DependentPairs.Add Item:=newPair, Key:=newPair.Name
        Next i
        
        Set newPair = Nothing
    End Sub
    
    Private Sub ActiveDependentPair_Change()
        Dim myStr As String
        With ActiveDependentPair
            myStr = "Active Pair: " & .Name
            myStr = myStr & vbCr & "Master: " & vbTab & .MasterBox.Name
            myStr = myStr & vbCr & "Dependent: " & vbTab & .DependentBox.Name
            myStr = myStr & vbCr & "Master Index: " & .MasterListIndex
            myStr = myStr & vbTab & "Dependent Index: " & .DependentListIndex
            myStr = myStr & vbCr & "Master Value: " & .MasterText
            myStr = myStr & vbTab & "Dependent Value: " & .DependentValue
        End With
        Label1.Caption = myStr
    End Sub
    
    Private Sub ActiveDependentPair_DependentChange()
        Me.Caption = ActiveDependentPair.DependentBox.Name & " has changed"
    End Sub
    
    Private Sub ActiveDependentPair_MasterChange()
        Me.Caption = ActiveDependentPair.MasterBox.Name & " has changed"
    End Sub
    Attached Files Attached Files
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  8. #8
    Registered User
    Join Date
    01-26-2016
    Location
    Houston, Texas
    MS-Off Ver
    2010
    Posts
    4

    Re: Repeating dependent ComboBoxes in a Userform

    Thanks mikerickson. Its going take me a bit to wrap my head around this. I will try it this weekend and post back. Thank you again!

  9. #9
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Repeating dependent ComboBoxes in a Userform

    The Creeping Feature Creature overcame me on that version. It got a bloated and cumbersome. This is leaner.

    The custom Class clsDependentPairs has properties:

    MasterBox
    '-- the control that determines which DependentList is shown in the DepenedentBox.
    '-- MasterBox can be either a ComboBox or a ListBox

    DependentBox
    '-- the control that shows the DependentList, depending on which item is selected from the MasterBox.
    '-- DependentBox can be either a ComboBox or a ListBox

    MasterList is a pass-through property that acts like .MasterBox.List
    DependentList(index) is what is shown when MasterBox.ListIndex = Index.
    '-- Typicaly DependentList(index) is a one dimensional array.
    '-- If DependentList(index) = Null, then the DependentBox will be cleared when MasterBox.ListIndex = Index
    '-- If DependentList(index) = "nc" or "No Change" (or any string), setting MasterBox.ListIndex to index will not change the list in DependentBox.
    '-- index can be -1. DependentBox will show DependentList(-1) when nothing is selected in the MasterBox.

    Note that either the MasterBox or the DependentBox can be multi-column. They cannot be multi-select.

    It will also pass a Change event to the (optional) ChangedPair variable in the Userform. The DependentChanged argument will be True/False depending on whether the ChangedPair.DependentBox or ChangedPair.Masterbox was changed.

    In the attached, the Userform_Intialize event shows one way to match master controls with their dependent control.
    The ChangedPair_Change event fills Label1 with information.

    ' in userform code module
    
    Dim DependentPairs As Collection
    Public WithEvents ChangedPair As clsDependentPair
    
    Private Sub UserForm_Initialize()
        Dim mList As Variant, dList0 As Variant, dList1 As Variant, dList2 As Variant
        Dim NewPair As clsDependentPair
        Dim MasterBoxes As Variant
        Dim DepBoxes As Variant
        Dim i As Long
    
        Set DependentPairs = New Collection
        mList = Array("PartA", "PartB", "PartC")
        dList0 = Array("FMA_1", "FMA_2", "FMA_3")
        dList1 = Array("FMB_1", "FMB_2", "FMB_3")
        dList2 = Array("FMC_1", "FMC_2", "FMC_3")
        
        MasterBoxes = Array(Me.ComboBox1, Me.ListBox1, Me.ComboBox3, ComboBox5, ComboBox7, ComboBox9, ComboBox11)
        DepBoxes = Array(ComboBox2, Me.ListBox2, ComboBox4, ComboBox6, ListBox3, ComboBox10, ComboBox12)
        
        For i = LBound(MasterBoxes) To UBound(MasterBoxes)
            Set NewPair = New clsDependentPair
            
            With NewPair
                Set .MasterBox = MasterBoxes(i)
                Set .DependentBox = DepBoxes(i)
                .MasterList = mList
                
                .DependentList(0) = dList0
                .DependentList(1) = dList1
                .DependentList(2) = dList2
            End With
            
            DependentPairs.Add Item:=NewPair, Key:=NewPair.Name
        Next i
        
        Set NewPair = Nothing
    End Sub
    
    Private Sub ChangedPair_Change(DependentChanged As Boolean)
        Dim strDisplay As String
    
        With ChangedPair
            strDisplay = .Name
    
            If DependentChanged Then
                strDisplay = strDisplay & vbCr & "Dependent (" & .DependentBox.Name & ") was changed."
            Else
                strDisplay = strDisplay & vbCr & "Master (" & .MasterBox.Name & ") was changed."
            End If
    
            strDisplay = strDisplay & vbCr & vbCr & "Master:" & vbTab & "index=" & .MasterBox.ListIndex
                strDisplay = strDisplay & vbTab & "value=""" & .MasterBox.Text & """"
    
            strDisplay = strDisplay & vbCr & "dependent:" & vbTab & "index=" & .DependentBox.ListIndex
                strDisplay = strDisplay & vbTab & "value=""" & .DependentBox.Text & """"
    
        End With
    
        Label1.Caption = strDisplay
    End Sub
    Attached Files Attached Files
    Last edited by mikerickson; 01-30-2016 at 03:32 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 13
    Last Post: 10-20-2019, 05:39 AM
  2. [SOLVED] Dependent ComboBoxes in Userform, then Submit Button
    By markusvirus in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-04-2015, 04:58 PM
  3. Two comboBoxes dependent on one another (userform)
    By fionamb83 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-20-2013, 05:18 AM
  4. [SOLVED] Building dependent comboboxes on an existing userform
    By Sway1978 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-19-2013, 11:11 AM
  5. [SOLVED] Dependent comboboxes
    By ObiWanBaloney in forum Excel General
    Replies: 6
    Last Post: 06-04-2012, 08:40 PM
  6. dependent comboboxes
    By humboldtguy in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 05-14-2010, 10:40 PM
  7. UserForms & dependent ComboBoxes?
    By hit_king51 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-11-2010, 09:30 PM

Tags for this Thread

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