excel - 未选择的组合框当前值的 ListIndex

标签 excel vba combobox

在 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/

相关文章:

Excel VBA : How do I open an Excel file without running macro but want macros to run when I activate workbook?

javascript - VBA动态网页抓取Excel

vba - Access 2013 运行时中未显示启动窗体

java - GWT ComboBox 值更改处理程序

excel查找返回引用

excel - 运行时的对象属性

VBA Msgbox - 无默认按钮

sql - 运行时错误: Invalid parameter 1 specified for datepart - VBA - SQL

c# - 将ComboBox的SelectedItem设置为ListCollectionView中的项目值

WPF DataGridTemplateColumn 与 ComboBox 绑定(bind)(MVVM 模式)