【问题标题】:Multiple Dependent Comboboxes Excel VBA多个从属组合框Excel VBA
【发布时间】:2019-10-24 22:27:13
【问题描述】:

Four Comboboxes

我有四个组合框。我用

初始化第一个
Fill DC1ComboBox
With DC1
    .AddItem "Door Open/Close"
    .AddItem "Jacket On/Off"
    .AddItem "Cycle Over"
    .AddItem "Alarm"
    .AddItem "Keycard Reader"
End With

第二个组合框选项取决于第一个选择,我根据 DC1.ListIndex 使用 Select Case 所做的,效果很好。

我已经开始使用第三个组合框(取决于第二个,取决于第一个),我认为必须有一种比嵌套选择案例更好的方法,因为它只是当我到达第 4 个盒子时,要长得愚蠢。

对于我要做的事情,夹克、自行车和警报只能在四个框中选择一次,门和钥匙卡可以选择两次。示例:Dry Contact 3 Selection Example

这是我开始的嵌套选择案例的剪辑:Nested Select Case

【问题讨论】:

    标签: excel vba combobox


    【解决方案1】:

    与使用案例不同,每次选择某个选项时增加变量可能更容易,然后检查该选项被选择了多少次。下面的 sub 用于 ComboBox #1,您可以对 #2 和 #3 使用类似的 sub。

    Public jacket As Integer
    Public alarm As Integer
    Public cycle As Integer
    Public door As Integer
    Public keycard As Integer
    
    Private Sub ComboBox1_Change()
    
    'check value of combobox and increment variables
    If ComboBox1.Value = "Jacket On/Off" And lastValue <> ComboBox1.Value Then
        jacket = jacket + 1
    End If
    
    If ComboBox1.Value = "Cycle Over" And lastValue <> ComboBox1.Value Then
        cycle = cycle + 1
    End If
    
    If ComboBox1.Value = "Alarm" And lastValue <> ComboBox1.Value Then
        alarm = alarm + 1
    End If
    
    If ComboBox1.Value = "Keycard Reader" And lastValue <> ComboBox1.Value Then
        keycard = keycard + 1
    End If
    
        If ComboBox1.Value = "Door Open/Close" And lastValue <> ComboBox1.Value Then
        door = door + 1
    End If
    
    'check totals of variables and add correct items
    With ComboBox2
    
        .clear
    
        If jacket < 1 Then
        .AddItem "Jacket On/Off"
        End If
    
        If cycle < 1 Then
        .AddItem "Cycle Over"
        End If
    
        If alarm < 1 Then
        .AddItem "Alarm"
        End If
    
        If keycard < 2 Then
        .AddItem "KeyCard"
        End If
    
        If door < 2 Then
        .AddItem "Door Open/Close"
        End If
    
    End With
    
    'disable ComboBox after use
    ComboBox1.Enabled = False
    
    End Sub
    

    请注意,我们必须在选择后禁用 ComboBox,因为如果有人返回 ComboBox 并重新选择更多项目,这种递增数学将不起作用。

    如果您想编写它而不必按预定顺序填写框,那么不使用类将非常困难。如果你有兴趣,这里有一个关于如何使用它们的链接:http://www.cpearson.com/excel/classes.aspx

    【讨论】:

      猜你喜欢
      • 2019-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-07
      • 2013-03-12
      • 1970-01-01
      相关资源
      最近更新 更多