简体   繁体   English

Excel VBA 从工作表填充父子下拉源

[英]Excel VBA to populate parent child drop down source from sheet

I have sheet with data like below and 2 combo boxes in user form with the names ComboBox1(parent) and ComboBox2(child)我有如下数据表和用户表单中的 2 个组合框,名称为 ComboBox1(parent) 和 ComboBox2(child)

Parent |Child
-------|------
  A    |   1
  A    |   2
  A    |   3
  X    |   100
  X    |   101
  X    |   102

With below code I took distinct values into Parent combo.使用下面的代码,我将不同的值带入了 Parent 组合中。

Dim s As String, r As Integer, nr As Integer, wr, v
Set wr = Sheet1.Range("A1:A10")
nr = wr.Rows.Count
With ComboBox1
 .Clear
 For r = 1 To nr
  v = wr(r, 1)
  If InStr(s, v & ",") = 0 Then
   s = s & v & ","
   .AddItem (v)
  End If
 Next
End With

I am looking help to populate child combo based on selected value in Parent combo!我正在寻找帮助来根据父组合中的选定值填充子组合!

I recommend to use a Dictionary to remember which entries in ComboBox1 already exists because you can easily test with dict.Exists(NewItem) .我建议使用Dictionary来记住 ComboBox1 中的哪些条目已经存在,因为您可以轻松地使用dict.Exists(NewItem)测试。

And the Data for ComboBox2 needs to automatically re-initialize on the ComboBox1_Change event.并且 ComboBox2 的数据需要在ComboBox1_Change事件上自动重新初始化。

Note that you should use more descriptive variable names s, r, nr, wr, v are pretty meaningless, while names like ComboRange1 or NewItem are pretty good readable for humans.请注意,您应该使用更具描述性的变量名称s, r, nr, wr, v毫无意义,而像ComboRange1NewItem这样的名称对人类来说可读性很好。 Makes your life much easier and results in less errors.使您的生活更轻松,并减少错误。

Option Explicit

Sub InitComboBox1()
    Dim ComboRange1 As Range
    Set ComboRange1 = Sheet1.Range("A1", Sheet1.Cells(Rows.Count, "A").End(xlUp))

    ComboBox1.Clear

    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    Dim i As Long
    For i = 1 To ComboRange1.Rows.Count
        Dim NewItem As String
        NewItem = ComboRange1.Cells(i, 1).Text

        If Not dict.Exists(NewItem) Then
            dict.Add NewItem, 0
            ComboBox1.AddItem NewItem
        End If
    Next i
End Sub

Private Sub ComboBox1_Change()
    Dim ComboRange2 As Range
    Set ComboRange2 = Sheet1.Range("A1", Sheet1.Cells(Rows.Count, "A").End(xlUp)).Resize(ColumnSize:=2)

    ComboBox2.Clear

    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    Dim i As Long
    For i = 1 To ComboRange2.Rows.Count
        If ComboRange2.Cells(i, 1).Text = ComboBox1.Value Then
            Dim NewItem As String
            NewItem = ComboRange2.Cells(i, 2).Text

            If Not dict.Exists(NewItem) Then
                dict.Add NewItem, 0
                ComboBox2.AddItem NewItem
            End If
        End If
    Next i
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel 或 VBA - 从另一个工作表中的动态列填充下拉列表,没有重复项 - Excel or VBA - populate a drop down list from a dynamic column in another sheet with no duplicates 从工作表B填充工作表A-Excel VBA - populate sheet A from sheet B - excel VBA 从主表填充Excel子表 - Populate Excel Child Sheets From Master Sheet 使用VBA从数据范围条件填充Excel数据验证下拉列表 - Populate Excel Data Validation Drop-Down From Data Range Condition Using VBA 使用 VBA 的下拉菜单自动填充 Excel 中的字段 - To Auto-Populate Fields in Excel Using a Drop-Down with VBA 通过下拉列表将数据从一张纸导出到另一张纸-VBA Excel Macro - Export data from one sheet to another via drop down list - VBA Excel Macro 如何使用VBA在Excel中读取工作表上的下拉列表的值 - How to read the value of a drop down list on a sheet in Excel using VBA vba中如何获取excel下拉列表源 - How to get excel drop down list source in vba 将 Excel VBA 代码集成到 Java 代码中,最终创建从 Excel 表格单元格中的下拉列表中选择多个值的功能 - Integrate Excel VBA code to Java code to finally create the feature of selecting multiple values from the drop down list in a cell of an excel sheet 使用单元格下拉列表中的值填充 vba ComboBox - Populate a vba ComboBox with the values from the drop-down list of a cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM