- All values in the combo boxes are derived from a worksheet within the same workfile. Each combo box corresponds to a specific column in that worksheet.
- Ideally, the combo boxes should interact with each other to filter their options. However, currently, they are not filtering properly. Instead, they’re displaying all unique values from their respective columns.
- The combo box named cboFilterFoodProduct is supposed to provide a final selection from column B in the worksheet, based on the filtered values from the other combo boxes. However, it’s not functioning correctly; it doesn’t allow me to select anything from it.
I expect that each combo box filters its options based on the selections made in the other combo boxes. Ultimately, I anticipate that the combo box named cboFilterFoodProduct will present a final value from its list, taking into account the filtered choices from the other combo boxes.
Here the current code:
Public comboBoxCaller As String
Private Sub PopulateComboBoxFromRange(ByVal comboBox As comboBox, ByVal data As Range)
Dim uniqueItems As Scripting.Dictionary
Set uniqueItems = New Scripting.Dictionary
Dim cell As Range
For Each cell In data
If Not cell.Value = "" And Not uniqueItems.Exists(cell.Value) Then
uniqueItems.Add cell.Value, Nothing
End If
Next cell
comboBox.Clear
Dim key As Variant
For Each key In uniqueItems.keys
comboBox.AddItem key
Next key
End Sub
Private Sub PopulateComboBoxFromColumn(ByVal comboBox As comboBox, ByVal column As String)
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Nutritional_Value_Database")
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, column).End(xlUp).Row
Dim uniqueItems As New Scripting.Dictionary
Dim cell As Range
For Each cell In ws.Range(column & "2:" & column & lastRow)
If Not cell.Value = "" And Not uniqueItems.Exists(cell.Value) Then
Call uniqueItems.Add(cell.Value, Nothing)
End If
Next cell
comboBox.Clear
Dim key As Variant
For Each key In uniqueItems.keys
comboBox.AddItem key
Next key
End Sub
Private Sub PopulateComboBoxFromKeys(ByVal comboBox As comboBox, ByVal keys As Variant)
comboBox.Clear
Dim key As Variant
For Each key In keys
comboBox.AddItem key
Next key
End Sub
Private Sub cboProductType_Change()
End Sub
Private Sub UserForm_Initialize()
comboBoxCaller = ""
PopulateComboBoxFromColumn cboProductType, "A"
PopulateComboBoxFromColumn cboCountryOrigin, "J"
PopulateComboBoxFromColumn cboStoreAvailability, "K"
End Sub
Private Sub UpdateFilter()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Nutritional_Value_Database")
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Dim productType As String
Dim countryOrigin As String
Dim storeAvailability As String
productType = Trim(cboProductType.Value)
countryOrigin = Trim(cboCountryOrigin.Value)
storeAvailability = Trim(cboStoreAvailability.Value)
Dim filteredProduct As New Scripting.Dictionary
Dim filteredCountry As New Scripting.Dictionary
Dim filteredStore As New Scripting.Dictionary
Dim foodProducts As New Scripting.Dictionary
Dim i As Long
For i = 2 To lastRow
Dim prodVal As String
Dim countryVal As String
Dim storeVal As String
Dim foodProdVal As String
prodVal = ws.Cells(i, "A").Value
countryVal = ws.Cells(i, "J").Value
storeVal = ws.Cells(i, "K").Value
foodProdVal = ws.Cells(i, "B").Value
If prodVal Like "*" & productType & "*" Or productType = "" Then
If Not filteredProduct.Exists(prodVal) Then filteredProduct.Add prodVal, Nothing
End If
If countryVal Like "*" & countryOrigin & "*" Or countryOrigin = "" Then
If Not filteredCountry.Exists(countryVal) Then filteredCountry.Add countryVal, Nothing
End If
If storeVal Like "*" & storeAvailability & "*" Or storeAvailability = "" Then
If Not filteredStore.Exists(storeVal) Then filteredStore.Add storeVal, Nothing
End If
If (prodVal Like "*" & productType & "*" Or productType = "") And
(countryVal Like "*" & countryOrigin & "*" Or countryOrigin = "") And
(storeVal Like "*" & storeAvailability & "*" Or storeAvailability = "") Then
If Not foodProducts.Exists(foodProdVal) Then foodProducts.Add foodProdVal, Nothing
End If
Next i
If comboBoxCaller <> "cboProductType" Then UpdateComboBox cboProductType, filteredProduct.keys
If comboBoxCaller <> "cboCountryOrigin" Then UpdateComboBox cboCountryOrigin, filteredCountry.keys
If comboBoxCaller <> "cboStoreAvailability" Then UpdateComboBox cboStoreAvailability, filteredStore.keys
UpdateComboBox cboFilterFoodproduct, foodProducts.keys
End Sub
Private Sub UpdateComboBox(ByVal comboBox As MSForms.comboBox, ByVal keys As Variant)
Dim current As String
current = comboBox.Value
comboBox.Clear
Dim key As Variant
For Each key In keys
comboBox.AddItem key
Next key
If Not IsError(Application.Match(current, comboBox.List, 0)) Then
comboBox.Value = current
ElseIf comboBox.ListCount > 0 Then
comboBox.ListIndex = 0 ' Optionally set to the first item if no current value is valid
End If
End Sub
Userfrom screenshot
New contributor
Felix Schubert is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.