列表框EXCEL的搜索栏

时间:2019-09-27 13:31:56

标签: excel vba

我有一个用户表单,其中有一个列表框,该表单在打开表单时会自动填充excel表行,

lstDisplay.ColumnCount = 11
lstDisplay.RowSource = "A1:L65356"

我需要添加一个搜索栏来更新列表框,并仅显示包含我搜索的值的行/行。 现在,我添加了一个文本框Textfind和一个按钮查找。老实说,我不知道该怎么做。

我尝试过类似的事情:

Private Sub find_Click()
Dim ws As Worksheet
Dim numRow As Integer
Dim found As Boolean
Set ws = ThisWorkbook.Worksheets("resdata")
For numRow = (ws.Range("A" & ws.Rows.Count).End(xlUp).Row)
        If nameTxtBox.Value = 

    (NO IDEA WHAT I CAN USE HERE )

            found = True
            Exit For
        End If
Next numRow
If found = False Then
    MsgBox "No Match Found!", vbCritical
    lstDisplay.Clear

End If

End Sub

谢谢大家。

1 个答案:

答案 0 :(得分:1)

您可以尝试以下方法:

   Private Sub find_Click()
Dim ws As Worksheet
Dim numRow As Integer
Dim found As Boolean

Dim MyRng As Range

Set MyRng = Range("A1:A" & Sheets("resdata").Cells(Sheets("resdata").Rows.Count, "A").End(xlUp).Row)


Set ws = ThisWorkbook.Worksheets("resdata")

        For Each C In MyRng
          If C.Value Like nameTxtBox.Value Then

            found = True
            Exit For
          End If
        Next

If found = False Then
    MsgBox "No Match Found!", vbCritical
    lstDisplay.Clear

End If

End Sub