VBA excel-2010 listbox1.SelectedIndex 抛出错误

VBA excel-2010 listbox1.SelectedIndex throws error

您好,我在 excel 工作簿中,在同一个工作表中,我写了 VBA 以从列表框中删除当前选定的项目,但函数 SelectedIndex 表示它不是列表框。我不确定为什么会这样,因为我看到很多示例都使用了它。任何帮助都是极好的!

代码:

ListBox1.Items.RemoveAt (ListBox1.SelectedIndex)

使用

ListBox1.RemoveItem ListBox1.ListIndex

您可能想添加一个关于实际选择的任何项目的检查:

With Me.ListBox1
    If .ListIndex <> -1 Then .RemoveItem .ListIndex
End With