简体   繁体   中英

Filter combobox list as you type Excel VBA

I've got the below code to filter a ComboBox list as you type.

Private Sub ComboBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

    Dim arrIn As Variant, arrOut As Variant
    Dim i As Long, j As Long

    arrIn = Sheet1.Range("A2:G7")

    ReDim arrOut(1 To UBound(arrIn), 1 To 2)
    For i = 1 To UBound(arrIn)
        If arrIn(i, 1) Like ComboBox1.Text & "*" Then
            j = j + 1
            arrOut(j, 1) = arrIn(i, 1)
            arrOut(j, 2) = arrIn(i, 2)
        End If
    Next

    ComboBox1.List = arrOut

End Sub

However, I need to make changes the range for array as Range("A2:A2000") accordingly I also have to make some changes here as well ReDim arrOut(1 To UBound(arrIn), 1 To 2) .

I'm unable to understand as I'm bad in array concepts and modifying them. Can anyone can help me?

Thank you in advance:)

If I understand your question correctly then the following code will place the values from Range("A2:A2000") into the array and then filter the ComboBox accordingly:

Private Sub ComboBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Dim arrIn As Variant, arrOut As Variant
    Dim i As Long, j As Long

    arrIn = Sheet1.Range("A2:A2000")

    ReDim arrOut(1 To UBound(arrIn), 1 To 1)

    For i = 1 To UBound(arrIn)
        If arrIn(i, 1) Like ComboBox1.Text & "*" Then
            j = j + 1
            arrOut(j, 1) = arrIn(i, 1)
        End If
    Next
    ComboBox1.List = arrOut
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM