首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >组合框搜索,第二个字符串,行之间搜索VBA

组合框搜索,第二个字符串,行之间搜索VBA
EN

Stack Overflow用户
提问于 2016-09-14 10:43:29
回答 1查看 175关注 0票数 0

目前,我正在使用VBA中的用户表单处理一个数据库。Userform有一个从工作表加载数据的组合框。工作表的值将会更改,这就是我动态创建它的原因。

问题是,这个组合框太长了。例如,如果我想搜索“example engine",我会键入"comb”,它会显示完整的输入。到目前为止,这个方法工作得很好。但是你总是需要知道单元格值的起始位置。当我只输入" engine“时,就不会有匹配项,因为没有其他条目以engine开头。我只有一列数据,那么有没有什么解决方案可以只通过输入引擎来显示“内燃机”呢?这只是一个简单的例子。数据列表包括许多单元格,这些单元格包含多个单词。

有谁知道我的问题的解决方案吗?我会非常感激的!提前谢谢你,索菲

EN

回答 1

Stack Overflow用户

发布于 2016-09-14 13:49:56

假设combobox是以"ComboBox1“命名的,您可以在用户表单代码窗格中尝试以下代码

代码语言:javascript
复制
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 Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39481724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档