Filter(Array(Range))不适用于populatin User_Form Combo-Box

时间:2017-02-02 09:58:32

标签: excel-vba vba excel

我想在VB userform中创建一个通配符搜索类型组合框。 搜索目标位于excel列。

我可以用:

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

但使用时失败:

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

运行时错误' 13'

     

类型不匹配

为什么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 个答案:

答案 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