Filter combobox list as you type

eduzs

Well-known Member
Joined
Jul 6, 2014
Messages
704
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Does anyone know how to implement a "filter as you type" in a combobox list in a userform vba?

Thanks
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi,

This should get you started:
Code:
Private Sub ComboBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Call Combobox1_Populate
End Sub

Private Sub UserForm_Initialize()
    Call Combobox1_Populate
End Sub

Sub Combobox1_Populate()

    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
The Combobox1_Populate code starts from a range on Sheet1 and puts that information into an array.
That data is then read row by row and if it matches the text entered into the ComboBox then it is copied to the output array.
That array is used to populate the ComboBox.

The Combobox1_Populate is called when the UserForm is initialized and everytime a key is released in the ComboBox.

I used a ComboBox with two columns and populate it from the worksheet. You can change both those to suit.
It works best if the ComboBox MatchEntry property is set to "2-fmMatchEntryNone" otherwise it guesses the word and the filter only finds one entry.
 
Upvote 0
If you need only a single column in the ComboBox then it works out more easily:
Code:
Sub Combobox1_Populate(Optional fltr As String)
    ComboBox1.List = Filter(Array("qqq", "qqwww", "qqttt", "qwer"), fltr)
End Sub

Private Sub ComboBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Call Combobox1_Populate(ComboBox1.Text)
End Sub

Private Sub UserForm_Initialize()
    Call Combobox1_Populate
End Sub
This time I assumed that the ComboBox list would be supplied in the code ao that a one dimensional array could be used permitting the Filter method to filter the list.

As you can see, the answer optimum will depend on your exact requirements.
 
Upvote 0
Thank you!
I'll try these solutions and update the post later.
 
Upvote 0
Thanks,

I made this code that works great, it's a workaround. Not perfect and beautiful, but seems to work.

ListaAT is a array populated before show the userform.

Code:
Private Sub CB1_Change()

Dim x As Integer

With CB1

    For x = 0 To UBound(ListaAT)

        If UCase(.Value) = UCase(ListaAT(x)) And Len(.Value) >= 1 Then

            Call AutoTexto.Roda_AutoTexto
            Exit Sub

        End If

    Next x

    .Visible = False

    Do While .ListCount > 0

        .RemoveItem (0)

    Loop

    For x = 0 To UBound(ListaAT)

        If .Value = "" Then

            .AddItem ListaAT(x)

        Else

            If UCase(Left(ListaAT(x), Len(.Value))) = UCase(.Value) Then .AddItem ListaAT(x)

        End If

    Next x

    .Visible = True
    .SetFocus
    .DropDown

End With

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,137
Messages
6,123,253
Members
449,093
Latest member
Vincent Khandagale

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top