在 VBA 中,组合框的 ListIndex 属性显示组合框中值的整个列表(数组)中所选项目的索引(从 0 开始)。如果没有进行选择,则显示 -1。
当我在 Excel 中打开带有组合框及其最后一个值的工作表时,它会显示 ListIndex 为 -1,而不是该项目的实际 ListIndex。
VBA中有什么技巧可以快速获取当前未选中值的ListIndex?
我知道我可以自己手动检查数组(.List 属性),但我希望 VBA 有一些更快的方法来执行此操作。
最佳答案
来自 ListIndex 属性的 VBA 帮助:
The ListIndex property contains an index of the selected row in a list. Values of ListIndex range from –1 to one less than the total number of rows in a list (that is, ListCount – 1). When no rows are selected, ListIndex returns –1. When the user selects a row in a ListBox or ComboBox, the system sets the ListIndex value.
所以我假设您没有选择任何内容,而是尝试读取所选值。这也许可以解释为什么 ListIndex 返回 -1。
关于excel - 未选择的组合框当前值的 ListIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3312185/