在键入时以vba格式在combox中过滤列表

时间:2015-10-15 07:09:05

标签: vba excel-vba combobox word-vba excel

我遇到如何在组合框中应用过滤器并查询记录的问题。

我正在使用名为Customers的组合框,其中包含名为Settings.customerArray的数组中所有客户的名称(约3000个)

我想在组合框中输入文本时自动缩小列表,  所以我的想法是以某种方式调整this方法来实现我想要的。我的工作代码看起来不像这样,不能正确刷新表单。

Private Sub Project_ComboBoxCustomer_Change()
    Dim e, temp
    With Me
        temp = .Project_ComboBoxCustomer.Value
        If Not .Project_ComboBoxCustomer.MatchFound Then
            .Project_ComboBoxCustomer.Clear
            If Len(temp) Then
                For Each e In CPearson.getColumnFrom2DArray(Settings.prosjektCustomerArray, 1)
                    If (e <> "") * (e Like "*" & temp & "*") Then
                        .Project_ComboBoxCustomer.AddItem e
                    End If
                Next
                If .Project_ComboBoxCustomer.ListCount > 0 Then
                    .Project_ComboBoxCustomer.ListIndex = 0
                End If
            End If
        End If
    End With
End Sub

有没有人对vba中的表单有任何类似问题的例程(我需要它在excel和word中工作,因此在excel表中使用数组而不是范围),或建议我如何改进当前算法。 / p>

以下是getColumnFrom2DArray的代码,如果有人需要它。它从2维数组中返回一维数组:

Function getColumnFrom2DArray(Arr As Variant, ColumnNumber As Long) As String()
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' GetColumn
    ' This populates ResultArr with a one-dimensional array that is the
    ' specified column of Arr. The existing contents of ResultArr are
    ' destroyed. ResultArr must be a dynamic array.
    ' Returns True or False indicating success.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim RowNdx As Long
    Dim ResultArr() As String
    ''''''''''''''''''''''''''''''
    ' Ensure Arr is an array.
    ''''''''''''''''''''''''''''''
    If IsArray(Arr) = False Then
        getColumnFrom2DArray = Array()
        Exit Function
    End If

    ''''''''''''''''''''''''''''''''''
    ' Ensure Arr is a two-dimensional
    ' array.
    ''''''''''''''''''''''''''''''''''
    If NumberOfArrayDimensions(Arr) <> 2 Then
        getColumnFrom2DArray = Array()
        Exit Function
    End If


    ''''''''''''''''''''''''''''''''''''
    ' Ensure ColumnNumber is less than
    ' or equal to the number of columns.
    ''''''''''''''''''''''''''''''''''''
    If UBound(Arr, 2) < ColumnNumber Then
        getColumnFrom2DArray = Array()
        Exit Function
    End If

    If LBound(Arr, 2) > ColumnNumber Then
        getColumnFrom2DArray = Array()
        Exit Function
    End If


    ReDim ResultArr(LBound(Arr, 1) To UBound(Arr, 1))
    For RowNdx = LBound(ResultArr) To UBound(ResultArr)
        ResultArr(RowNdx) = Arr(RowNdx, ColumnNumber)
    Next RowNdx

    getColumnFrom2DArray = ResultArr
End Function

0 个答案:

没有答案