【问题标题】:Excel 2013 VBA - ActiveX Cascading ComboBoxes - Issue with having only related values in cmb4Excel 2013 VBA - ActiveX Cascading ComboBoxes - cmb4 中只有相关值的问题
【发布时间】:2015-08-06 20:39:54
【问题描述】:

我想要实现的是级联或依赖组合框,在帮助下,我终于在所有 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

我的所有代码都位于工作表“图表”上。 我的所有数据都位于工作表“数据”上 我所有的组合框都位于“图表”

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

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

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

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

    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

如果您想了解更多背景知识,请查看此链接:Excel '13 VBA Cascading ComboBox - Trouble getting unique values in Combobox2

【问题讨论】:

    标签: excel combobox activex cascadingdropdown vba


    【解决方案1】:

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

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

    检查将哪些值放入 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
    

    【讨论】:

    • 首先,再次感谢您的回复。但是,我确实了解每条线路在做什么,我只是不完全了解如何在没有他人帮助的情况下让他们做到这一点。我已经尝试过更改您提到的部分,例如更改 myVal 引用的内容,但将所有内容放回原处我第一次拥有它是为了在这里发帖。正如我在原始帖子中提到的,我觉得我需要同时引用子类别和位置。非常感谢您的帮助!
    • 顺便说一句,你是个漂亮的男人,成功了!!我已经断断续续地在这方面工作,同时尝试学习近 1.5 个月。进入项目的下一部分!
    • 我并不想做出评判……我只是想提出一些可以帮助您学习的建议,以便您将来能够编辑内容。我确实注意到您对您认为自己需要什么的评论,但没有 cmets 说明您尝试过什么以及您需要帮助哪些行。我很乐意提供帮助,因为我知道不是每个人都有经验,而且我见过有类似问题的 CS 专业的学生,​​只是想提供一些建议。
    • 语气不是文字最容易传达的!我真的很感激。
    猜你喜欢
    • 2015-10-29
    • 2015-01-26
    • 1970-01-01
    • 2022-11-11
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多