ComboBox with autocomplete dropdown OR add to list

jimmisavage

Board Regular
Joined
Jun 28, 2017
Messages
130
Hi,
I have a list in column A and ComboBox1 is in Userform1

When a user begins to type in the combobox I would like a list of 3 options to dropdown filtered on what they're typing. If the user continues to type and it's not in the list I would like what the user is typing to be added to the list.

So, if i have a list of names in column A and the user types 'STE' the dropdown list would show Stephen, Steven and Steve. I would also like it if the user typed 'EVE' it would show Eve, StEVEn, StEVE etc so the search would search the whole word, not just the beginning.

I have tried a few different options in the combobox but I can't get it to work as above. Can anyone help?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
What have you tried?

The closest I could find was this but I didn't have much success with it
Code:
[COLOR=#252C2F][FONT=Courier]Private Sub ComboBox1_Change()[/FONT][/COLOR]    Dim e, temp
    With Me
        temp = .ComboBox1.Value
        If Not .ComboBox1.MatchFound Then
            .ComboBox1.Clear
            If Len(temp) Then
                For Each e In Sheets("sheet1").Cells(1).CurrentRegion.Value
                    If (e <> "") * (e Like "*" & temp & "*") Then
                        .ComboBox1.AddItem e
                    End If
                Next
                If .ComboBox1.ListCount > 0 Then
                    .ComboBox1.ListIndex = 0
                End If
            End If
        End If
    End With [COLOR=#252C2F][FONT=Courier]End Sub[/FONT][/COLOR]
 
Last edited:
Upvote 0
You probably want to look at using Filter.
Code:
Dim arrIn As Variant ' inital list
Private Sub ComboBox1_Change()
Dim arrOut As Variant
    
    arrOut = Filter(arrIn, ComboBox1.Value, True)
    
    If UBound(arrOut) <> -1 Then
        ComboBox1.List = arrOut
    Else
        ComboBox1.List = arrIn
    End If
End Sub

Private Sub UserForm_Initialize()
    ComboBox1.List = Range("A1").CurrentRegion.Value
    arrIn = Application.Transpose(ComboBox1.List)
End Sub
 
Upvote 0
I found this thread here which seems to give a pretty close solution to what I need but it throws up an error when i start typing. The error highlights Me.ComboBox1.Clear but I can't understand why. That's the name of my combobox!

It also doesn't have the part where it would add to my list if it's not already there.
 
Upvote 0
You probably want to look at using Filter.
Code:
Dim arrIn As Variant ' inital list
Private Sub ComboBox1_Change()
Dim arrOut As Variant
    
    arrOut = Filter(arrIn, ComboBox1.Value, True)
    
    If UBound(arrOut) <> -1 Then
        ComboBox1.List = arrOut
    Else
        ComboBox1.List = arrIn
    End If
End Sub

Private Sub UserForm_Initialize()
    ComboBox1.List = Range("A1").CurrentRegion.Value
    arrIn = Application.Transpose(ComboBox1.List)
End Sub

This is messing up a few things for me. I added the code inside my bombobox. However, when i click on my button to launch the userform it hightlights UserForm1.Show as an error on my button.

I have no idea what's going on there!
 
Upvote 0
What I posted is an example that I tested using a simple userform with a combobox populated from column A, it's not really mean to be just plugged into exisiting code.:)

What code do you have in your userform and what errors are you getting?
 
Upvote 0
What I posted is an example that I tested using a simple userform with a combobox populated from column A, it's not really mean to be just plugged into exisiting code.:)

What code do you have in your userform and what errors are you getting?

That's exactly what I've done as well. Only I added a button to load the userform to test. I currently have no code in my userform and added your code to my combobox
 
Upvote 0
What happens if you run the userform manually from the VBE rather than the button?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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