I can’t get dependent combo boxes filtered to according to what has been selected in other combo boxes

  • 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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật