Populate other combo boxes based on previous combo boxes selection

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Currently on a user form, I have 6 combo boxes. The first combo box gets its values from a spreadsheet. The first combo box pulls from the code below. What I am wanting to do is to have the other combo boxes remove any name that was already chosen from the previous combo box. Is this possible? Thank you.

See code below to see how its populated.
VBA Code:
Private Sub UserForm_Initialize()
    ComboBox1.RowSource = "Employees!A2:A" & Range("A" & Rows.Count).End(xlUp).Row
End Sub
 
As you stated, changing .value to .text fixed the runtime error. So my question is, how do you know when to use .value vs .text? Is it because we used "Option Explicit" at the beginning of this program? Thank you again.
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
As you stated, changing .value to .text fixed the runtime error. So my question is, how do you know when to use .value vs .text? Is it because we used "Option Explicit" at the beginning of this program? Thank you again.
It has nothing to do with "Option Explicit".
AFAIK .text and .value as the property of combobox can be used interchangeably, actually it's the first time I find this kind of problem, and as I said I still can't figure out why.
So, to be safe just use the .Text property.
 
Last edited:
Upvote 0
@Akuini
Is there away to apply the code in the previous post to work on multiselect ListBoxes and after the selections are made, apply it to the ComboBoxes.
Let me explain this in a real world application. I have to two MultiSelect ListBoxes(ListBox1 and ListBox2). (ListBox1=PTO (Vacation)(ListBox2=SickTime). I have this line of code to populate each ListBox located in the UserForm Initialize at the very bottom. Now I just need to figure out how to update a ListBox2 when using the multiselect option in ListBox1? And then apply that to all the ComboBoxes. So the multiselect listboxes is the second set of controls the user encounters before the comboBoxes. If anyone calls in sick the day before or has a scheduled vacation, then they are selected from the designated listBox. If they are on vacation, then they are removed from listBox2 and all comboboxes that would follow. If you should have any questions, please let me know. Thank you.


VBA Code:
With Me.ListBox1
        Call toPopulate(1)
        Call toPopulate(2)
    End With

VBA Code:
Private Sub UserForm_Initialize()
    Dim i As Integer
    Dim ctrl As Control
 

    cmb1 = Me.ComboBox1.Value
    cmb2 = Me.ComboBox2.Value
    cmb3 = Me.ComboBox3.Value
    cmb4 = Me.ComboBox4.Value
    cmb5 = Me.ComboBox5.Value
    cmb6 = Me.ComboBox6.Value
    cmb7 = Me.ComboBox7.Value
    cmb8 = Me.ComboBox8.Value
    cmb9 = Me.ComboBox9.Value
    cmb10 = Me.ComboBox10.Value
    cmb11 = Me.ComboBox11.Value
    cmb12 = Me.ComboBox12.Value
    cmb13 = Me.ComboBox13.Value
    cmb14 = Me.ComboBox14.Value
    cmb15 = Me.ComboBox15.Value
    cmb16 = Me.ComboBox16.Value
    cmb17 = Me.ComboBox17.Value
    cmb18 = Me.ComboBox18.Value
    cmb19 = Me.ComboBox19.Value
    cmb20 = Me.ComboBox20.Value
    cmb21 = Me.ComboBox21.Value
    cmb22 = Me.ComboBox22.Value
    cmb23 = Me.ComboBox23.Value
    cmb24 = Me.ComboBox24.Value
    cmb25 = Me.ComboBox25.Value
    cmb26 = Me.ComboBox26.Value
    cmb27 = Me.ComboBox27.Value
    cmb28 = Me.ComboBox28.Value
    cmb29 = Me.ComboBox29.Value
    cmb30 = Me.ComboBox30.Value
    cmb31 = Me.ComboBox31.Value
    cmb32 = Me.ComboBox32.Value
   
    Sheets("Sheet1").Activate
    Range("A1:F1").ClearContents
    Range("A3:F3").ClearContents
    Range("A5:F8").ClearContents
    Range("A10:F15").ClearContents
    Range("B16:F18").ClearContents
   
    Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare
    Set e = CreateObject("scripting.dictionary"): e.CompareMode = vbTextCompare
   
    With Sheets("Employees")
        va = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
    End With
   
       
    With Me.ListBox1
        Call toPopulate(1)
        Call toPopulate(2)
    End With
   
End Sub

I tried to reuse the same code with a little bit of modification but it did not work.

VBA Code:
Sub toPopulate(n As Long)
    
    Dim i As Long, l As Long
    Dim tx As String
    Dim X
    
    d.RemoveAll
    e.RemoveAll
'********************************************************************************
    For l = 1 To 2
        tx = UserForm1.Controls("ListBox" & l).Text
        If tx <> "" And l <> n Then e(tx) = Empty
    Next
    If e.Count <> 0 Then
        For Each X In va
            If Not e.Exists(X) Then d(X) = Empty
        Next
    Else
        For Each X In va
            d(X) = Empty
        Next
    End If
'*********************************************************************************
    For i = 1 To 32
        tx = UserForm1.Controls("ComboBox" & i).Text
        If tx <> "" And i <> n Then e(tx) = Empty
    Next
    
    If e.Count <> 0 Then
        For Each X In va
            If Not e.Exists(X) Then d(X) = Empty
        Next
    Else
        For Each X In va
            d(X) = Empty
        Next
    End If
    
    UserForm1.Controls("ComboBox" & n).List = d.keys
    UserForm1.Controls("ListBox" & n).List = d.keys

End Sub
 
Upvote 0
Could you upload a sample workbook?
Could you explain what you're trying to do in more detail, step by step?
What should happen when you open the userform? Populating all the combobox & listbox with same data? then what?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top