我正在摆弄 VBA,尤其是列表框。
我创建了一个搜索按钮,它使用查找属性 (.Find) 并搜索我们在其数据表的文本框中输入的任何内容。
它可以将搜索结果添加到列表框中,但是如果我们想要搜索结果所在的整行,我们如何添加它,而不是像这样 vba listbox multicolumn add ?
使用 VBA 如何完成?
这可能吗?
另外,不用VBA也可以吗?
发生了什么:
Data table: a | b | c | d | e
1 | 2 | 3 | 4 | 5
Search: a
Listbox : a
Search: 1
Listbox : 1
Search: 2
Listbox : 2
Search: d
Listbox : d
我“想要”:
Data table: a | b | c | d | e
1 | 2 | 3 | 4 | 5
Search: a
Listbox: a | b | c | d | e
Search: 1
Listbox: 1 | 2 | 3 | 4 | 5
Search: 2
Listbox: 1 | 2 | 3 | 4 | 5
Search: d
Listbox: a | b | c | d | e
如果需要,我可以提供我用于测试搜索的代码。
最佳答案
可以使用 .List
成员访问多列列表框中的项目。该成员接受两个参数作为输入:
ListBox1.List(RowIndex, ColumnIndex)
RowIndex:我们要访问的记录的行索引。请注意,列表框中的行是从零开始的。因此第一行的索引是0。
ColumnIndex:我们要访问的字段的列索引。请注意,多列列表框中的列也是零索引的。因此,第一列的索引为 0。
例如:
'Assign values to the columns
For i = 1 To 9
ListBox1.AddItem 'Create a new row
ListBox1.List(i - 1, 0) = i 'Fill the first column
ListBox1.List(i - 1, 1) = i * 10
ListBox1.List(i - 1, 2) = i * 100
ListBox1.List(i - 1, 3) = i * 1000
Next i
或者
but if we want the entire row from where search result is, how do we add it, instead of doing something like this vba listbox multicolumn add ?
假设您有一个代表 .Find
结果的范围对象(例如,rngFound
):
Me.ListBox1.Clear
Me.ListBox1.ColumnCount = rngFound.Columns.Count
Me.ListBox.RowSource = rngFound.Address
关于vba - 在多列列表框中添加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33522788/