I wanted to use ListBox so that I can select the entry rather than TextBox

前端 未结 1 644
无人及你
无人及你 2021-01-26 11:48

Excel 2013 with vba:

I have 2 columns in Sheet1 Column A has NBA Players while Column B shows their Jersey Numbers. If I type 2 in txtNumber it will display Players with

相关标签:
1条回答
  • 2021-01-26 12:02

    Assuming the name of the listbox is ListBox1 then you may try something like this...

    Private Sub txtNumber_Change()
    Dim mySheet As Worksheet    'declaring mySheet as the Worksheet...
    Dim x, dict
    Dim i As Long
    Dim cnt As Long
    Set mySheet = Sheets("Sheet1")
    ListBox1.Clear
    x = mySheet.Range("A1").CurrentRegion.Value
    Set dict = CreateObject("Scripting.Dictionary")
    If Application.CountIf(mySheet.Columns(2), txtNumber.Value) > 0 Then
        For i = 2 To UBound(x, 1)
            If x(i, 2) = Val(txtNumber.Value) Then
                dict.Item(x(i, 1)) = ""
            End If
        Next i
        ListBox1.List = dict.keys
    Else
        ListBox1.AddItem "Match not found"
    End If
    End Sub
    
    0 讨论(0)
提交回复
热议问题