Excel 2013 VBA - ActiveX 级联组合框 - 在 cmb4 中只有相关值的问题

Excel 2013 VBA - ActiveX Cascading ComboBoxes - Issue with having only related values in cmb4

我想要实现的是级联或依赖组合框,在帮助下,我终于在所有 4 个方面都取得了成功。

ComboBox1 = Category
ComboBox2 = Sub Category
ComboBox3 = Location (unique to chosen subcategory)
ComboBox4 = Customer (unique to chosen subcategory and location)

在组合框 4 中,所选位置的所有客户都在填充组合框 4,而不是所选位置的所有客户都与子类别一致。

ComboBox1 = cmbRent
ComboBox2 = cmbSub
ComboBox3 = cmbLoc
ComboBox4 = cmbCust

我的所有代码都位于工作表 "CHART" 上。 我的所有数据都位于工作表 "DATA" 我所有的组合框都位于 "CHART"

引用的数据按框的顺序分 4 列。

Column1 = Category
Column2 = Sub Category
Column3 = Location 
Column4 = Customer

我觉得我需要在 cmbSub 和 cmbLoc 中引用 Selection 才能实现我想要的?

这是我应用于工作表的所有组合框代码

    Private Sub cmbRent_Change()

    Dim wsChart As Worksheet
    Dim wsData As Worksheet
    Dim listOfValues As String 'To store list of values already added
    Dim ValueToAdd As String 'To store new value to add
    listOfValues = ""
    Set wsChart = ThisWorkbook.Sheets("CHART")
    Set wsData = ThisWorkbook.Sheets("DATA")


    MyVal = Me.cmbRent.Value

    'loop thru col B
    lr = ThisWorkbook.Sheets("DATA").Cells(Rows.Count, 1).End(xlUp).Row

    'clear cmbSub
        ThisWorkbook.Sheets("CHART").cmbSub.Clear


     For x = 2 To lr
    If MyVal = wsData.Cells(x, 1) Then
       'add to combobox
        ValueToAdd = wsData.Cells(x, 2) 'Get value from worksheet
        If InStr(listOfValues, wsData.Cells(x, 2)) = 0 Then
        'Check to see if the value has already been added
        'If not, add to values added and add the item to the combobox.
              listOfValues = listOfValues & ValueToAdd
              Me.cmbSub.AddItem ValueToAdd
        End If
    End If
    Next x

          ThisWorkbook.Sheets("CHART").cmbSub.ListIndex = -1
    End Sub

        Private Sub cmbSub_Change()

    Dim wsChart As Worksheet
    Dim wsData As Worksheet
    Dim listOfValues As String 'To store list of values already added
    Dim ValueToAdd As String 'To store new value to add
    listOfValues = ""
    Set wsChart = ThisWorkbook.Sheets("CHART")
    Set wsData = ThisWorkbook.Sheets("DATA")

    MyVal = ThisWorkbook.Sheets("CHART").cmbSub.Value

    'loop thru col c
    lr = wsData.Cells(Rows.Count, 2).End(xlUp).Row

          ThisWorkbook.Sheets("CHART").cmbLoc.Clear

    For x = 2 To lr
    If MyVal = wsData.Cells(x, 2) Then
       'add to combobox
        ValueToAdd = wsData.Cells(x, 3) 'Get value from worksheet
        If InStr(listOfValues, wsData.Cells(x, 3)) = 0 Then
        'Check to see if the value has already been added
        'If not, add to values added and add the item to the combobox.
              listOfValues = listOfValues & ValueToAdd
              ThisWorkbook.Sheets("CHART").cmbLoc.AddItem ValueToAdd
        End If
    End If
    Next x

        ThisWorkbook.Sheets("CHART").cmbLoc.ListIndex = -1


      End Sub


      Private Sub cmbLoc_Change()

    Dim wsChart As Worksheet
    Dim wsData As Worksheet
    Dim listOfValues As String 'To store list of values already added
    Dim ValueToAdd As String 'To store new value to add
    listOfValues = ""
    Set wsChart = ThisWorkbook.Sheets("CHART")
    Set wsData = ThisWorkbook.Sheets("DATA")

    MyVal = ThisWorkbook.Sheets("CHART").cmbLoc.Value

    'loop thru col D
    lr = wsData.Cells(Rows.Count, 3).End(xlUp).Row

          ThisWorkbook.Sheets("CHART").cmbCust.Clear

    For x = 2 To lr
    If MyVal = wsData.Cells(x, 3) Then
       'add to combobox
        ValueToAdd = wsData.Cells(x, 4) 'Get value from worksheet
        If InStr(listOfValues, wsData.Cells(x, 4)) = 0 Then
        'Check to see if the value has already been added
        'If not, add to values added and add the item to the combobox.
              listOfValues = listOfValues & ValueToAdd
              ThisWorkbook.Sheets("CHART").cmbCust.AddItem ValueToAdd
        End If
    End If
     Next x

        ThisWorkbook.Sheets("CHART").cmbCust.ListIndex = -1


    End Sub

如果您想了解更多背景信息,请查看此 link:

问题是您没有在代码中对子类别进行比较。

您遇到的一个更大的问题是您似乎不理解代码的作用。我会花一些时间来浏览您的代码并尝试理解每一行在做什么。可能再次观看您在其他帖子中引用的视频。

检查将哪些值放入 combobox4(也称为 cmbCust)的代码部分位于此处:

If MyVal = wsData.Cells(x, 3) Then

这是检查 MyVal,它之前被定义为:

MyVal = ThisWorkbook.Sheets("CHART").cmbLoc.Value

这只是cmbLoc中的选择,对应位置,不包括子类。

你需要做两次检查,我会修改变量名,让它们更清楚。

Dim LocVal As String
Dim SubCatVal As String

....more code here

LocVal = ThisWorkbook.Sheets("CHART").cmbLoc.Value
SubCatVal = ThisWorkbook.Sheets("CHART").cmbSub.Value

....more code here


'Now do the comparison
If LocVal = wsData.Cells(x, 3) And SubCatVal = wsData.Cells(x,2) Then
    ValueToAdd = wsData.Cells(x, 4) 

.....Rest of code in the if statement