跳转到列表框中的项目

时间:2013-10-29 12:07:48

标签: vba listbox

有一个问题,有可能以某种方式跳转到特定列表框中的某个索引,如下图所示?

enter image description here

我已经尝试过以下代码

Listbox.ListIndex = index

但是它让我误以为 你错误地使用了ListIndex属性

我列表中的一个属性可能很重要。

行源类型:表/查询

提前谢谢。

2 个答案:

答案 0 :(得分:2)

试试ListBox.Selected(index) = True。如果它是一个多选列表框,您还需要遍历其他元素并以相同的方式取消选择它们。

答案 1 :(得分:1)

使用代码

创建标准模块
Sub Main()
    UserForm1.Show
    Unload UserForm1
End Sub

插入用户表单并直观地执行类似

的操作

enter image description here

进入用户格式代码并添加

Private Sub CommandButton1_Click()

    Dim v As Long
    For v = 0 To ListBox1.ListCount - 1
        If TextBox1 = ListBox1.List(v) Then
            ListBox1.Selected(v) = True
        End If
    Next v

End Sub

Private Sub UserForm_Initialize()

    With ListBox1
        .AddItem ("text1")
        .AddItem ("text2")
        .AddItem ("text3")
    End With

End Sub

运行Main

在框中输入:text2

将在列表中选择text2

enter image description here