0

我对组合框的内容有疑问。在我的用户表单上,有 3 个组合框。根据从组合框 1 中选择的项目,组合框 2 应该显示集合 1 或集合 2。

组合框 3 的内容也会发生同样的情况,这取决于组合框 1 和 2 中所选项目的组合。

但是,我遇到了组合框 2 的问题,它总是由集合 2 填充,即使我在组合框 1 中选择了应该在第二个组合框中生成集合 1 的项目。

这是我使用的代码:

Private Sub UserForm_Initialize()
    With ComboBox1
        .Clear
        .AddItem "In contrast"
        .AddItem "Eng?"
        .AddItem "Trillers"
        .AddItem "Natuur(lijk)"
        .AddItem "Muziektrafiek"
    End With

    If ComboBox1.Value = "In contrast" Then
        GoTo LineComboBox1Set1
    End If

    If ComboBox1.Value = "Eng?" Then
        GoTo LineComboBox1set2
    End If

    If ComboBox1.Value = "Trillers" Then
        GoTo LineComboBox1set2
    End If

    If ComboBox1.Value = "Natuur(lijk)" Then
        GoTo LineComboBox1set2
    End If

    If ComboBox1.Value = "Muziektrafiek" Then
        GoTo LineComboBox1set2
    End If


LineComboBox1Set1:
     With ComboBox2
        .Clear
        .AddItem "Op verkenning"
        .AddItem "Gehoord? Gezien?"
        .AddItem "On stage"
        .AddItem "Creabende"
        .AddItem "Ingeblikt"
     End With

LineComboBox1set2:
     With ComboBox2
        .Clear
        .AddItem "Op verkenning"
        .AddItem "Gehoord? Gezien?"
        .AddItem "On stage"
        .AddItem "Creabende"
        .AddItem "Ingeblikt"
        .AddItem "Speak up"
        .AddItem "In de kijker"
    End With

谁能帮我解决这个问题?

非常感谢提前!!

亲切的问候,马克

4

1 回答 1

0

It will do as your code does not end when you use the goto statement. I.e. you send the code to LineComboBox1set1 and the code executes that and then continues to run each line of code as there is nothing stoping it!

The quick fix is just to add "Exit Sub" after each "End With" but if it was my code I would refactor it by using a SELECT CASE switch i.e.

SELECT CASE ComboBox1.Value
     Case "In contrast"
           With ComboBox2
                Etc.....
           End With
     Case "Eng?", "Thriller" and so on
           With ComboBox2
                Stuff for set 2
           End With
  End Select
于 2010-04-19T10:27:15.627 回答