Dependent Combobox Problem

amoverton2

Board Regular
Joined
May 13, 2021
Messages
77
Office Version
  1. 2016
Platform
  1. Windows
I have a userform with 4 comboboxes, I would like them to be dependent comboboxes with an advanced filter used in the process to whittle down the selection by the 4th box.

I have been watching videos and reading tutorials but I can't find exactly what I want, however I did find one but it returns an error that is not seen in the tutorial video.

Here is the code that returns the error:

Private Sub UserForm_Initialize()

Dim sh As Worksheet
Set sh = Sheets("Pool2")

Dim i As Long
For i = 2 To sh.Range("A10000").End(x1Up).Row (this is usually what the debugger picks on)

If Application.WorksheetFunction.CountIf(sh.Range("A2", "A" & i), sh.Cells(i, 1)) = 1 Then
Me.ComboBox1.AddItem sh.Cells(i, 1)
End If
Next i

End Sub

Here is a snippet of the data being used (the max is 600 rows). The idea is for someone to pick the code, then the rate, then if they qualified, and the last combobox is the name with no duplicates in any of the comboboxes. If there is a way to do it without using an advanced filter that'll be cool. In the original worksheet the columns used below are not A,B,C,D but D (Code), B (Rate), K (Qualified), C (Name). Sorry to make it hard...

Cell Formulas
RangeFormula
A2:A50A2='[Future Sponsorship List.xlsm]Enlisted Sponsor Pool'!D5
B2:B50B2='[Future Sponsorship List.xlsm]Enlisted Sponsor Pool'!B5
C2:C50C2='[Future Sponsorship List.xlsm]Enlisted Sponsor Pool'!K5
D2:D50D2='[Future Sponsorship List.xlsm]Enlisted Sponsor Pool'!C5



Thank you, I really do appreciate the help, and if anyone can tell me why the code I post is returning the error in simple terms that'll be great.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Could you upload a sample workbook (without sensitive data) to a sharing site like dropbox.com or google drive?
And then share the link here.
This will make it easier to test & find a solution.
 
Upvote 0

Data and the Userform are shared. Edit at will. However, other sheets referenced in the userform code are not included (i.e. Search and Update) because they are not the focus.
 
Upvote 0
For i = 2 To sh.Range("A10000").End(x1Up).Row

If this was copy pasted, typo in xlUp. You wrote x1Up.
 
Upvote 0
Try this:

I made some changes in Sub UserForm_Initialize() and on Change & Enter event of:
Me.cmbBBDCODE
Me.cmbSPONRATE
Me.cmbQUAL
Me.cmbSPONNAME
and add Sub toChange()

Part of the code does this:
if a combobox changes, then clear the next combobox. It's to prevent incorrect data on all combobox.
For example:

VBA Code:
Private Sub cmbBBDCODE_Change()
    Me.cmbSPONRATE.Value = ""
    Me.cmbQUAL.Value = ""
    Me.cmbSPONNAME.Value = ""
    Call toChange
End Sub

The file:
 
Upvote 0
Try this:

I made some changes in Sub UserForm_Initialize() and on Change & Enter event of:
Me.cmbBBDCODE
Me.cmbSPONRATE
Me.cmbQUAL
Me.cmbSPONNAME
and add Sub toChange()

Part of the code does this:
if a combobox changes, then clear the next combobox. It's to prevent incorrect data on all combobox.
For example:

VBA Code:
Private Sub cmbBBDCODE_Change()
    Me.cmbSPONRATE.Value = ""
    Me.cmbQUAL.Value = ""
    Me.cmbSPONNAME.Value = ""
    Call toChange
End Sub

The file:
This is awesome, thank you so much!!!

Can I ask one more favor?

How can I set the comboboxes to sort alphabetically (cmbSPONRATE and cmbSPONNAME) order or numerical order (then alphabetical, for the cmbQUAL) when entered?
 
Upvote 0
How can I set the comboboxes to sort alphabetically (cmbSPONRATE and cmbSPONNAME) order or numerical order (then alphabetical, for the cmbQUAL) when entered?
You need a helper column, I'm using col Z in sheet "Enlisted Sponsor Pool". You can adjust it here:
VBA Code:
'sheet's name where the helper column is located.
Private Const sList As String = "Enlisted Sponsor Pool"

'the helper column, first cell
Private Const xH As String = "Z1"

If you want cmbBBDCODE.List to be sorted then use this line:
cmbBBDCODE.List = toSort(d.Keys)
but if col Code is always sorted then no need to sort it again, so use this line:
cmbBBDCODE.List = d.Keys
in this part:
VBA Code:
Private Sub cmbBBDCODE_Enter()
.....   
    cmbBBDCODE.List = d.Keys
'    cmbBBDCODE.List = toSort(d.Keys)

The file:
 
Upvote 0
How does the info in Z1 & Z2 (the helper column) get there? Or do I just put two values in alphabetical order or is it from the private const? --> just trying to understand, thanks for your patience!
 
Upvote 0
You need a helper column, I'm using col Z in sheet "Enlisted Sponsor Pool". You can adjust it here:
VBA Code:
'sheet's name where the helper column is located.
Private Const sList As String = "Enlisted Sponsor Pool"

'the helper column, first cell
Private Const xH As String = "Z1"

If you want cmbBBDCODE.List to be sorted then use this line:
cmbBBDCODE.List = toSort(d.Keys)
but if col Code is always sorted then no need to sort it again, so use this line:
cmbBBDCODE.List = d.Keys
in this part:
VBA Code:
Private Sub cmbBBDCODE_Enter()
.....  
    cmbBBDCODE.List = d.Keys
'    cmbBBDCODE.List = toSort(d.Keys)

The file:

I have another challenge that I'm having a hard time figuring out and you probably know how to do it.

How can I display everything that is on the Enlisted Sponsor Pool sheet in a listbox (in userform ESP) with dependent combo boxes to filter data where the listbox shows only shows the filtered data.

However this is not like before where I wanted specific data, I would like to be able to filter all data on the sheet.
For example; first filter is UIC, next filter is Paygrade, next filter is Code, next filter Qualified. I only need a max of 4 filters.

With new form with ListBox
 
Upvote 0
How does the info in Z1 & Z2 (the helper column) get there? Or do I just put two values in alphabetical order or is it from the private const? --> just trying to understand, thanks for your patience!
In "Sub UserForm_Initialize" the code populates the defined range (col D,B,K,C) into variable va,vb,vc,vd.
Then, for example, when you enter cmbSPONRATE, vb will be filtered by va value that match cmbBBDCODE.Value. The filtered value is assigned to var d (dictionary object) to make it unique.
The process is in this part:

Private Sub cmbSPONRATE_Enter()
.....
For i = LBound(va) To UBound(va)
If UCase(va(i, 1)) = UCase(Me.cmbBBDCODE.Value) Then d(vb(i, 1)) = Empty
Next

Me.cmbSPONRATE.List = toSort(d.Keys)

Then to sort the value in d, the code sends d values to a helper column (pointed by constant xH, in this example is Z1, well Z1 actually is a cell but it's easier to code by using the first cell in the helper column) then sort it. And then the values in the helper column is populated to cmbSPONRATE.List. The process is in Function toSort.

The helper column is a temporary helper column, its values is changed depend on which combobox is changed.
.....

is it from the private cons?
Yes

I have another challenge that I'm having a hard time figuring out and you probably know how to do it.
It's a different problem, you need to start a new thread. ;)
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,548
Members
449,038
Latest member
Guest1337

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