目前,我正在使用VBA中的用户表单处理一个数据库。Userform有一个从工作表加载数据的组合框。工作表的值将会更改,这就是我动态创建它的原因。
问题是,这个组合框太长了。例如,如果我想搜索“example engine",我会键入"comb”,它会显示完整的输入。到目前为止,这个方法工作得很好。但是你总是需要知道单元格值的起始位置。当我只输入" engine“时,就不会有匹配项,因为没有其他条目以engine开头。我只有一列数据,那么有没有什么解决方案可以只通过输入引擎来显示“内燃机”呢?这只是一个简单的例子。数据列表包括许多单元格,这些单元格包含多个单词。
有谁知道我的问题的解决方案吗?我会非常感激的!提前谢谢你,索菲
发布于 2016-09-14 13:49:56
假设combobox是以"ComboBox1“命名的,您可以在用户表单代码窗格中尝试以下代码
Private Sub ComboBox1_Change()
Dim i As Long
Static found As Boolean '<--| this will be used as this sub "footprint" and avoid its recursive and useless calls
If found Then '<-- we're here just after the text update made by the sub itself, so we must do nothing but erase our "footprint" and have this sub run at the next user combobox change
found = False '<--| erase our "footprint"
Exit Sub '<--| exit sub
End If
With Me.ComboBox1 '<--| reference userform combobox
If .Text = "" Then Exit Sub '<--| exit if no text has been typed in
For i = 0 To .ListCount - 1 '<--|loop through its list
If InStr(.List(i), .Text) > 0 Then '<--| if current list value contains typed in text...
found = True '<--| leave our "footprint"
.Text = .List(i) '<--| change text to the current list value. this will trigger this sub again but our "footprint" will make it exit
Exit For '<--| exit loop
End If
Next i
End With
End Subhttps://stackoverflow.com/questions/39481724
复制相似问题