从TextBox文本中仅筛选ListBox Excel中的匹配结果

我有一个文本框和一个列表框的用户表单。 我希望用户能够在文本框中input文本,并根据他们的input具有列表框筛选结果。

到目前为止,我设法让列表框突出显示列表中的匹配结果,但不过滤掉不匹配的结果。 我也遇到了我的代码没有标识多个匹配logging的问题,不知道我需要添加什么来实现这一点。

Private Sub TextBox3_Change() 'searches ListBox3 for match and hightlights result. Need to filter results. Dim i As Long Dim sFind As String sFind = Me.TextBox3.Text If Len(sFind) = 0 Then Me.ListBox3.ListIndex = -1 Me.ListBox3.TopIndex = 0 Else For i = 0 To Me.ListBox3.ListCount - 1 If UCase(Left(Me.ListBox3.List(i), Len(sFind))) = UCase(sFind) Then Me.ListBox3.TopIndex = i Me.ListBox3.ListIndex = i Exit For End If Next i End If End Sub 

尝试使用这个代码,当你退出textbox3,否则它会做一些过滤,而打字时,可以带来错误。

如果比赛确切

 Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean) For i = ListBox1.ListCount - 1 To 0 Step -1 If Not ListBox1.List(i) = TextBox3 Then ListBox1.RemoveItem (i) Next i End Sub 

循环是由recursion循环构成的,否则会出现错误。

部分匹配

 Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean) For i = ListBox1.ListCount - 1 To 0 Step -1 If InStr(1, ListBox1.List(i), TextBox3) = 0 Then ListBox1.RemoveItem (i) Next i End Sub 

find一个更好的代码来过滤一个列表框。