(Excel VBA)如何将ComboBox的自动完成功能显示为DropDown列表

时间:2016-07-21 14:13:07

标签: vba excel-vba excel

我希望以下代码将值添加到组合框中,然后当用户将字符输入组合框时,组合框的下拉功能将仅显示包含这些字符的项目,类似于Google搜索栏的方式的工作原理。

Google Search Bar http://intersites.com/resources/uploads/suite_131/google_bar.png

代码编辑:

Option Explicit
Option Compare Text

Public LC As Long
Public Count As Integer
Dim ComboArray() As String

'Initializes the userform, and saves values from database into an array
Private Sub UserForm_Initialize()
LC = Cells(1, Columns.Count).End(xlToLeft).Column

ReDim ComboArray(1 To LC)

For Count = 1 To LC
    ComboArray(Count) = Cells(1, Count).Value
Next Count
End Sub

'Prevents changes if the down key is pressed?
Private Sub ComboBox_SiteName_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Application.ScreenUpdating = False
End Sub

'Adds values to combobox if they contain the string input by user
Private Sub ComboBox1_Change()
Dim pos As Integer
Dim i As Integer

ComboBox1.Clear

For Count = 1 To LC
    pos = InStr(1, ComboArray(Count), ComboBox1.Value)
    If pos <> 0 Then
        With ComboBox1
            .AddItem Cells(1, Count)
        End With
    End If
Next Count
End Sub

1 个答案:

答案 0 :(得分:0)

这是一个简单的示例,可能需要针对您的目的进行细化,但说明了使用KeyPress事件构建用户输入字符串的一般原则,并将其与列表中的每个项目进行比较,将列表过滤为输入字符串开头的值。

这需要一些改进来处理退格,删除等,这是我试图做的,但没有达到我想要的程度。

代码:

Option Explicit
Dim cbList As Variant
Dim userInput$

'### USERFORM EVENTS
Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Select Case KeyCode
        Case 8, 48
            'MsgBox "Backspace"
            Debug.Print "Backspace"
            If userInput <> "" Then
            userInput = Left(userInput, Len(userInput) - 1)
            End If
        Case 46
            'MsgBox "Delete"
            Debug.Print "Delete"
            userInput = Replace(userInput, ComboBox1.SelText, "")
    End Select
End Sub
Private Sub UserForm_Activate()
    Dim cl As Range
    userInput = ""
    For Each cl In Range("A1:A8")
        Me.ComboBox1.AddItem cl.Value
    Next
    Me.ComboBox1.MatchRequired = False
    cbList = Me.ComboBox1.List
End Sub
Private Sub UserForm_Terminate()
    userInput = ""
End Sub
'#### END USERFORM EVENTS
'#### COMBOBOX EVENTS

Private Sub ComboBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Me.ComboBox1.List = cbList
    ' Capture the user input in module variable
    userInput = userInput & Chr(KeyAscii)
    Debug.Print "input: " & userInput
    Debug.Print KeyAscii
    Dim i As Long, itm
    For i = Me.ComboBox1.ListCount - 1 To 0 Step -1
        itm = Me.ComboBox1.List(i)
        If Not StartsWith(CStr(itm), userInput) Then
            Me.ComboBox1.RemoveItem i
        End If
    Next

    If Me.ComboBox1.ListCount = 0 Then
        Me.ComboBox1.List = cbList
    Else
        Me.ComboBox1.List = Me.ComboBox1.List
    End If
    Me.ComboBox1.DropDown

End Sub
'#### END COMBOBOX EVENTS

'#### HELPER FUNCTIONS
Function StartsWith(imtVal$, inputStr$, Optional caseSensitive As Boolean = False)
', Optional caseSensitive As Boolean = False
    'If Not caseSensitive Then
        imtVal = LCase(imtVal)
        inputStr = LCase(inputStr)
    'End If
    StartsWith = VBA.Strings.Left(imtVal, Len(inputStr)) = inputStr
End Function
'#### END HELPER FUNCTIONS