设置Listindex后,Excel Listbox不一致地没有值 [英] Excel Listbox inconsistently has no value after setting the Listindex
问题描述
我有一个简单的用户窗体,其中包含两个列表框和两个文本框,其中包含当前所选Listitem的值.我有一个奇怪的错误,即在加载表单时其中一个框不会填充.如果我关闭该表单并再次加载它,则对面的框将不会加载.如果我关闭并第三次重新加载,则它将加载第一个框,但不会再次加载第二个框.重复广告恶心.
I have a simple UserForm with two Listboxes and two Textboxes that contain the value of the currently selected Listitem. I am having a bizarre bug where one of the boxes will not populate when the form loads. If I close the form and load it again, then the opposite box will not load. If I close and reload a third time, then it loads the first box but not the second again. Repeat ad nauseum.
首次加载:
第二次加载:
在启动时,代码中应同时填充两个文本框.该错误的根源是什么?
The code should have both Textboxes populated at startup. What is the source of this bug?
Private Sub UserForm_Initialize()
Dim i As Long
For i = 1 To 4
ListBox1.AddItem "Item A - " & i
Next i
ListBox1.ListIndex = 0
End Sub
Private Sub ListBox1_Change()
Dim i As Long
ListBox2.Clear
For i = 1 To 3
ListBox2.AddItem "Item B - " & i
Next i
ListBox2.ListIndex = 0
TextBox1.Value = ListBox1.Value
End Sub
Private Sub ListBox2_Change()
TextBox2.Value = ListBox2.Value
End Sub
推荐答案
这个答案来自Yow3Ek以及来自任何人.此代码按测试运行,没有错误或先前的问题.谢谢大家,我今天学到了一些东西.它正在开火.
This answer is from Yow3Ek as much as from anyone. This code runs as tested without error or previous problem. Thanks guys, I learned something today. It was firing on the clear.
Private Sub UserForm_Initialize()
Dim i As Long
For i = 1 To 4
Me.ListBox1.AddItem "Item A - " & i
Next i
Me.ListBox1.ListIndex = 0
End Sub
Private Sub ListBox1_Change()
Dim i As Long
Me.ListBox2.Clear
For i = 1 To 3
ListBox2.AddItem "Item B - " & i
Next i
Me.ListBox2.ListIndex = 0
Me.TextBox1.Value = Me.ListBox1.List(Me.ListBox1.ListIndex)
End Sub
Private Sub ListBox2_Change()
If Me.ListBox2.ListIndex = -1 Then Exit Sub
Me.TextBox2.Value = Me.ListBox2.List(Me.ListBox2.ListIndex)
End Sub
这篇关于设置Listindex后,Excel Listbox不一致地没有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!