【问题标题】:Excel VBA ComboBox2 doesn't get the right contentExcel VBA ComboBox2 没有得到正确的内容
【发布时间】:2011-02-08 20:18:19
【问题描述】:

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

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

但是,我遇到了组合框 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

谁能帮我解决这个问题?

提前非常感谢!

亲切的问候, 马克

【问题讨论】:

    标签: excel vba combobox population


    【解决方案1】:

    当您使用 goto 语句时,您的代码不会结束。 IE。您将代码发送到 LineComboBox1set1 并执行该代码,然后继续运行每一行代码,因为没有什么可以阻止它!

    快速解决方法是在每个“End With”之后添加“Exit Sub”,但如果这是我的代码,我会使用 SELECT CASE 开关重构它,即

    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
    

    【讨论】:

      猜你喜欢
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      • 1970-01-01
      • 1970-01-01
      • 2019-10-23
      • 2012-03-09
      相关资源
      最近更新 更多