我在尝试根据文本框中的搜索字符串过滤用户表单上的ListBox结果时遇到问题。我想最终链接一个组合框,它将选择哪个列将被搜索过滤器但是现在,因为我正在使用“RowSource”属性,它不允许我从ListBox中删除项目。
我知道有一个替代方法可以使用数组填充ListBox,但我遇到了列数超过10的问题,因为它是一个未绑定的列表,它会抛出错误消息。
这是我到目前为止的代码:
Private Sub UserForm_Initialize()
ListBoxCharges.RowSource = "SourceTable!A1:" & _ Sheets("SourceTable").Range("A1").SpecialCells(xlCellTypeLastCell).Address
With Me.ListBox1
.MultiSelect = fmMultiSelectMulti
.ColumnCount = Worksheets("SourceTable").UsedRange.Rows(1).Columns.Count
End With
With Me.comboboxFilter
For i = 1 To Me.ListBox1.ColumnCount
If Sheets("SourceTable").Cells(1, i).Value <> "" Then
.AddItem Sheets("SourceTable").Cells(1, i).Value 'ComboBox is populating from a horizontal headers list.
End If
Next i
End With
Private Sub txtboxFilter_Change()
Dim i As Long
Dim sCrit As String
'Add asterisks around text for all matches
'UCase is used to make filter case-insensitive
sCrit = "*" & Me.txtboxFilter.Text & "*"
With Me.ListBox1
.RowSource = "SourceTable!A1:" & Sheets("SourceTable").Range("A1").SpecialCells(xlCellTypeLastCell).Address
For i = .ListCount - 1 To 0 Step -1
If Not UCase(.List(i)) Like sCrit Then
Debug.Print .Selected(i)
.Selected(i) = False
.RemoveItem i
End If
Next i
.MultiSelect = fmMultiSelectMulti
.ColumnCount = Worksheets("SourceTable").UsedRange.Rows(1).Columns.Count
End With
End Sub
现在我在“.RemoveItem i”行上收到“未指定错误”(80004005)
我尝试用循环填充列表但是我收到错误“无法设置列表属性。无效的属性值”。我已经搜索过,这与未绑定的列表有关,并且它试图在第11个索引处添加值或类似的东西。
With Me.ListBox1
'assuming row 1 is the row you want to use to determine the columns in the listbox
column_count = Worksheets("SourceTable").UsedRange.Rows(1).Columns.Count
'configure listbox
.ColumnCount = column_count 'number of columns
For i = 1 To Sheets("SourceTable").Cells(Rows.Count, 1).End(xlUp).Row
.AddItem
For j = 1 To column_count 'columns are index based and start at 0, but j represents sheet column and we will use -1 to indicate listbox column
.List(i - 1, j - 1) = Worksheets("SourceTable").Cells(i, j) 'This is where I get the error "could not set the list property. invalid property value"
Next j
Next i
End With
我已经完成了代码,它将一直工作到j = 11然后我得到错误“.list(i-1,j-1)= wor ....
如果有人能引导我朝着正确的方向前进,那么我就错过了。
我真的希望有一些方法可以使用RowSource填充列表并在填充时对其进行过滤。
谢谢!
*** ****更新
我找到了一种过滤源数据然后将过滤后的列表添加到命名范围的方法,但是当我尝试将该命名范围添加为RowSource时,它给出了一个错误:
运行时错误'380': 无法设置RowSource属性。无效的属性值。
我认为这是因为命名范围是指非连续行,而RowSource正在查找没有跳过任何行的表?
遵循该路线是否有任何价值?
谢谢!