Custom RowSource for Combo Boxes

ilya2004

Board Regular
Joined
Mar 17, 2011
Messages
135
Hi All
Please see the attached example:

I have some list boxes that I am trying to fill with ranges from a spreadsheet.

I am building a form to help users browse employee data. I would like to be able to narrow the data down by country.

In the attached example, I would like the form to give the user the options of the countries that are in column 1. This should populate the data from column 2 with only the name of the employees that are in those countries.

The problem is that it has to be dynamic and the data can come in different orders, so I need it to be dynamic to read off the list.

Any ideas?
example.JPG
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
HI,

See if the following helps.
1. First you need to create an "ActiveX" command button in Sheet1 if thats the sheet you have the data. I used sheet1 you will need to change the "Sheet1" to the Sheet name you are using.

Paste the following code to sheet code module

Code:
Private Sub CommandButton1_Click()
Dim lrow As Long
Dim Mycel As Range
For Each Mycel In Sheets("Sheet1").Range("A2:A" & Range("A65536").End(xlUp).Row)
         UserForm1.ComboBox1.AddItem (Mycel)
Next
UserForm1.Show
End Sub

Now paste the following code to to the Userform code module.

Code:
Private Sub ComboBox1_Change()
Dim lrow As Long
Dim Mycel As Range
For Each Mycel In Sheets("Sheet1").Range("A2:A" & Range("A65536").End(xlUp).Row)
         If Cells(Mycel.Row, 1).Text = UserForm1.ComboBox1.Value Then
            UserForm1.ComboBox2.AddItem Cells(Mycel.Row, 2).Text
         End If
        
    Next
End Sub
 
Upvote 0
This is an excellent start!

I am have two difficulties.
The first box is repeating the country names. If Germany is on the list twice, it is listing it twice in the combo box.
Also, for some reason it is only filtering by country for the first selection I make. I.e. when I select "Germany" the first time, it only gives me "German" employees, but once I switch to "France" it populates the list with all of the names, not just "French".
I tried to upload the file to a google doc to share, but I am not sure if it worked. Here is the link if it did:

https://docs.google.com/leaf?id=0Bx1zPaEVJ3WuMDE1NGVkMjktNTcwYi00N2Y4LTgwZWMtNGM0NTU3OTFmMDM3&hl=en
 
Upvote 0
Ok,

Sorry for the problem in the following code I added a line for each code.
The new lines should clear the combobox before the new entry.

Code:
Private Sub ComboBox1_Change()
Dim lrow As Long
Dim Mycel As Range
UserForm1.ComboBox2.Clear ''added code here
For Each Mycel In Sheets("Sheet1").Range("A2:A" & Range("A65536").End(xlUp).Row)
         If Cells(Mycel.Row, 1).Text = UserForm1.ComboBox1.Value Then
            UserForm1.ComboBox2.AddItem Cells(Mycel.Row, 2).Text
         End If
        
    Next
End Sub

Code:
Private Sub CommandButton1_Click()
Dim lrow As Long
Dim Mycel As Range
UserForm1.ComboBox1.Clear ''added code here
For Each Mycel In Sheets("Sheet1").Range("A2:A" & Range("A65536").End(xlUp).Row)
         UserForm1.ComboBox1.AddItem (Mycel)
Next
UserForm1.Show
End Sub
 
Upvote 0
Great!

That fixed the problem with the employee name. Now only the correct employees are showing up. Do you have any ideas how to fix the first issue with the countries. Right now when I am clicking on the combo box, I still see:

<table border="0" cellpadding="0" cellspacing="0" width="137"><col style="width: 103pt;" width="137"> <tbody><tr style="height: 14.25pt;" height="19"> <td style="height: 14.25pt; width: 103pt;" width="137" height="19">United States</td> </tr> <tr style="height: 14.25pt;" height="19"> <td style="height: 14.25pt;" height="19">Germany</td> </tr> <tr style="height: 14.25pt;" height="19"> <td style="height: 14.25pt;" height="19">France</td> </tr> <tr style="height: 14.25pt;" height="19"> <td style="height: 14.25pt;" height="19">United States</td> </tr> <tr style="height: 14.25pt;" height="19"> <td style="height: 14.25pt;" height="19">France</td> </tr> <tr style="height: 14.25pt;" height="19"> <td style="height: 14.25pt;" height="19">Germany</td> </tr> <tr style="height: 14.25pt;" height="19"> <td style="height: 14.25pt;" height="19">United States</td> </tr> <tr style="height: 14.25pt;" height="19"> <td style="height: 14.25pt;" height="19">Germany</td> </tr> <tr style="height: 14.25pt;" height="19"> <td style="height: 14.25pt;" height="19">France</td> </tr> <tr style="height: 14.25pt;" height="19"> <td style="height: 14.25pt;" height="19">United States</td> </tr> </tbody></table>
Instead of just each country once.
 
Upvote 0
HI,

When you select the "Country" the name of the country will be displayed in the combobox. Now a question. Why would you want to show only the Country selected when you are looking for the "Employee" for that country. If the Country combo box will only show the selected Country how would you then select another Country?
 
Upvote 0
I see what you're saying. Sorry I wasn't clear.

What I want is for the first box to act as a filter for the values in the second box. That way, when I open the form, I am given a choice of 3 countries. Once I pick the country that I am operating under, I then get a limited list of only those employees that work in that country.

(In real application, this will then trigger pulling other data about this employee, but that part I don't need help with).
 
Upvote 0
HI,

Place this code in the sheet code module.

Code:
Private Sub CommandButton1_Click()
Dim Mycel
Dim a, z
a = Range("A2", Range("A" & Rows.Count).End(xlUp))
With CreateObject("scripting.dictionary")
    .comparemode = vbTextCompare
    For Each Mycel In a
        If Not IsEmpty(Mycel) Then
            If Not .exists(Mycel) Then .Add Mycel, Nothing
        End If
    Next
    z = .keys
End With
With UserForm1.ComboBox1
    .Clear
    .List = Application.Transpose(z)
End With
UserForm1.Show
End Sub
 
Upvote 0
Fantastic! It's always so much cleaner when someone knows that they are doing. (I don't even want to tell you what mine looked like).

Thanks very much for your help!
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,473
Members
452,915
Latest member
hannnahheileen

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