2016-09-14 84 views
0

此刻我正在使用VBA中的Userform處理數據庫。用戶窗體有一個組合框,用於從表單中加載數據。工作表的值會改變,這就是爲什麼我動態創建它。Combobox搜索,第二個字符串,在行之間搜索VBA

問題是,組合框很長。例如,如果我想搜索「內燃機」,我會輸入「梳子」,它會顯示整個輸入。到目前爲止,這完美地工作。但你總是需要知道細胞價值的開始。 當我輸入「引擎」時,將不會匹配,導致其他入口不會以引擎開始。我只有一個數據柱,因此有什麼解決方案可以顯示「內燃機」只能由輸入引擎? 那只是一個簡單的例子。數據列表包含許多包含多個單詞的單元格。

有誰知道我的問題的解決方案?我會很感激! 謝謝你在前進, 蘇菲

回答

0

假設組合框「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 
+0

@SophieUt,你通過它得到什麼? – user3598756

+0

@SophieUt,你會很高興給正在幫助你的人提供適當的反饋 – user3598756