Excel VBA ComboBox2 未获取正确的内容
我对组合框的内容有疑问。在我的用户表单上,有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它会起作用,因为当您使用 goto 语句时您的代码不会结束。即,您将代码发送到 LineComboBox1set1,代码执行该代码,然后继续运行每一行代码,因为没有什么可以阻止它!
快速修复只是在每个“End With”之后添加“Exit Sub”,但如果这是我的代码,我会使用 SELECT CASE 开关重构它,即
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.