I'm working on a search form for an Excel-based application. I'm using ComboBoxes where there will be up to 1000 entries in the list. I would like it to operate such that, when the user types in characters into the ComboBox, the box's list is automatically modified to show only those entries that match the characters entered. I have that much working with no problems, and the drop-down list changes as it should. However...
When the user directly selects any of the items in the list through the drop-down, either before or after typing in some characters, the .value property remains null and the box empty. It seems to be a problem with the box being re-entrant and the .index value changing, but I've been fighting this thing for a couple of days now and haven't gotten anywhere. Any ideas?
I've distilled the problem down to the code below. To replicate the problem:
Thanks in advance.
Excel 2019
Windows
When the user directly selects any of the items in the list through the drop-down, either before or after typing in some characters, the .value property remains null and the box empty. It seems to be a problem with the box being re-entrant and the .index value changing, but I've been fighting this thing for a couple of days now and haven't gotten anywhere. Any ideas?
I've distilled the problem down to the code below. To replicate the problem:
- Create a UserForm
- Drag a ComboBox onto the new form
- Rename the ComboBox to cbxData
- Paste the code below into the Code window
- Try to select one of the drop-down items, either before or after typing in characters to filter (e.g. a, p, b, *a, *w, etc)
Thanks in advance.
Excel 2019
Windows
VBA Code:
Dim aData As Variant
Private Sub cbxData_Change()
'On entry, all items from array aData appear in the dropdown box.
'As you enter letters into the combobox, only the items from aData which start with those letters will remain, and all others will be removed from the combobox list.
'Prefacing the data entry with an asterisk (*) will filter for all items in aData which have that sequence of characters after the * somewhere in the string (wildcard search).
'However, selecting any item from the drop-down box will result in cbxData.Value being null, rather than the item showing up in the box.
Dim bWildcardSearch As Boolean
Dim sSearchString As String
Dim i As Integer
Debug.Print "Me.cbxData.Value on entry = " & cbxData.Value
Debug.Print "Me.cbxData.ListIndex on entry = " & CStr(Me.cbxData.ListIndex)
If Left(Me.cbxData.Value, 1) = "*" Then
bWildcardSearch = True
sSearchString = Mid(Me.cbxData.Value, 2)
Else
bWildcardSearch = False
sSearchString = Me.cbxData.Value
End If
Me.cbxData.Clear
For i = 0 To UBound(aData)
If bWildcardSearch Then
If InStr(UCase(aData(i)), UCase(sSearchString)) > 0 Then Me.cbxData.AddItem aData(i)
Else
If UCase(Left(aData(i), Len(sSearchString))) = UCase(sSearchString) Then Me.cbxData.AddItem aData(i)
End If
Next i
Debug.Print "Me.cbxData.Value on exit = " & cbxData.Value
Debug.Print "Me.cbxData.ListIndex on exit = " & CStr(Me.cbxData.ListIndex)
End Sub
Private Sub UserForm_Initialize()
'cbxData Properties are left at default in the form editor and set below:
Me.cbxData.MatchEntry = fmMatchEntryNone
aData = Array("Apples", "Oranges", "Peaches", "Bananas", "Kiwi")
Me.cbxData.List = aData
End Sub