2017-02-02 20 views
1

我想在VB用戶窗體中製作通配符搜索類型組合框。 搜索目標位於excel列。過濾器(數組(範圍))不適用於普及User_Form組合框

我可以運行:

Sub Combobox1_Populate(Optional fltr As String) 
    ComboBox1.List = Filter(Array(Range("A1:A9")), fltr)** 
End Sub 

運行時錯誤 '13'

類型不匹配

Private Sub Combobox1_Populate(Optional fltr As String) 
    ComboBox1.List = Filter(Array("a", "ab", "abc", "abcd"), fltr) 
End Sub 

,但使用時失敗

爲什麼Filter(Array(Range無法正常工作?請幫助糾正它。


它下面也沒有問題。只是Array(Range當過濾器

Private Sub ComboBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) 
    Call Combobox1_Populate(ComboBox1.Text) 
End Sub 

Private Sub UserForm_Initialize() 
    Call Combobox1_Populate 
End Sub 
+1

'Range'總是返回一個二維數組和'Filter'只接受1D之一。使用'Application.Transpose'轉置範圍。 – Rory

+0

是這樣做的! – Jimmy

+0

感謝羅裏,我不知道,並總是與字典上的過濾器:/;) –

回答

0

合併不行試試下面的代碼:

Sub Combobox1_Populate(Optional fltr As String) 

    ComboBox1.List = Filter(Application.Transpose(Range("A1:A9")), fltr) 

End Sub 

'============================================================== 

Private Sub UserForm_Initialize() 

Call Combobox1_Populate("shai") '<-- add the filter string in brackets, I've tested with "shai" 

End Sub 
+0

是的!謝謝你太多了。 現在我有一個「通配符搜索類型組合框」 – Jimmy