Excel VBA ComboBox2 未获取正确的内容

发布于 2024-08-29 07:33:57 字数 1470 浏览 5 评论 0原文

我对组合框的内容有疑问。在我的用户表单上,有 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

任何人都可以帮我解决这个问题吗?

非常感谢!

亲切的问候, 马克

I'm having a problem with the content of a combobox. On my userform, there are 3 comboboxes.
Depending on the chosen item from combobox1, combobox2 should display either set 1 or set 2.

The same will be happening with the content of combobox 3, which depends upon the combination of chosen items from combobox 1 and 2.

However, I'm running into problems with combobox 2, which is always populated by set 2, even if I select the item in combobox 1 that should generate set 1 in the second combobox.

This is the code I used:

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

Can anyone help me on this one?

Thanks a lot in advance!!

Kind regards,
Marc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽清梦 2024-09-05 07:33:57

它会起作用,因为当您使用 goto 语句时您的代码不会结束。即,您将代码发送到 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文