【问题标题】:Combobox Search, Second String, Search between the lines VBA组合框搜索,第二个字符串,行间搜索 VBA
【发布时间】:2017-01-21 17:53:20
【问题描述】:

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

问题是,组合框很长。例如,如果我想搜索“燃烧引擎”,我将输入“comb”,它会显示整个输入。到目前为止,这非常有效。但是你总是需要知道单元格值的开始。 当我只键入“引擎”时,不会有匹配项,因为没有其他条目以引擎开头。我只有一个数据列,所以有什么解决方案可以显示,例如“内燃机”只靠输入引擎? 那只是一个简单的例子。数据列表包括许多包含多个单词的单元格。

有人知道我的问题的解决方案吗?我将不胜感激! 先感谢您, 苏菲

【问题讨论】:

    标签: string vba search combobox


    【解决方案1】:

    假设组合框以“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 Sub
    

    【讨论】:

    • @SophieUt,你通过了吗?
    • @SophieUt,很高兴你能向试图帮助你的人提供适当的反馈
    • 嗨@user3598756 - 我需要你的代码帮助,所以当我在字符串(狗、猫、兔子)中搜索值时它可以正常工作,所以如果我正在搜索猫,它会填充需要的整条线。但是当我尝试在组合框用户论坛中搜索 rabbit 时,它不会填充整行,除非我按 cat 前面的空格来填充整行,你能帮帮我吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 2019-02-12
    • 2023-03-24
    • 2017-02-09
    • 1970-01-01
    • 2013-03-30
    相关资源
    最近更新 更多