【发布时间】:2019-09-12 15:25:44
【问题描述】:
实际上,我只是在这里创建了自己的帐户,因为我无法找到当前 Excel VBA 问题的答案。
我正在创建一个带有 ListBox 的用户窗体,它通过一个多维数组填充。在某些情况下,单击 ListBox 后也会重新填充 ListBox,例如在该 List 的新行中(这会自动导致单击事件)。
参考我的问题的标题,我发现单击列表的新行时,前一行和所选行都带有下划线(即“单击”,即使 ListBox 的 MultiSelect 选项设置为“0-fmMultiSelectSingle”)。如果我请求 .ListIndex 值,它只会显示上一行的值(而不是现在新选择的行)。
但最大的问题是列表框的重新填充,只有 (!) 在列表的新行中单击(并创建此多选)时似乎不起作用。
我提供了我的情况的示例代码。当然,我的代码要复杂得多,所以我尝试将其缩短到主要方面。我很乐意向你们发送我的用户表单的屏幕截图,但数据是合理的(无论如何我希望你们能理解我的问题)。
问题:是否有解决方案或足够的解决方法来填充 ListBox 在以下情况下 - 通过点击事件 - 您在 ListBox 中选择了一个新行?
提前感谢您的热心帮助。我已经尝试和寻找了很长一段时间了;也许有人以前遇到过类似的问题!
-Graf_Rasen
Sub ListBox_Click()
intListIndex = Userform.ListBox.ListIndex
'When evaluating intListIndex in combination with clicking in a new row of 'the ListBox (which executes "Sub ListBox_Click"), intListIndex merely returns the value of the previously selected ListIndex
'defaults ListIndex, needs to be done for repopulation
Userform.ListBox.ListIndex = -1
'This does not work when changing into a new row in the ListBox
Userform.ListBox.List() = arr_DataListBox
'Redefines ListIndex
Userform.ListBox.ListIndex = intListIndex
End Sub
【问题讨论】:
-
我无法重现该问题。您确定没有通过任何地方的代码将其重置为
fmMultiSelectMulti?