【发布时间】:2015-06-13 02:48:27
【问题描述】:
我正在尝试让 VBA ComboBox 下拉并仅显示与键入的字符串匹配或部分匹配的项目。
为此,我设置了一个 ComboBox KeyUp 事件管理器,如下:
Public Sub TempCombo_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Select Case KeyCode
Case 9
'If TAB is pressed, then move one place right
ActiveCell.Offset(0, 1).Activate
Case 13
'If Enter is pressed, then move one place down
ActiveCell.Offset(1, 0).Activate
Case Else
'Otherwise, filter the list from the already entered text
Dim x As Long
OriginalValue = Me.TempCombo.Value
'Remove items from the ComboBox list
If Me.TempCombo.ListCount > 0 Then
For i = 1 To Me.TempCombo.ListCount
Me.TempCombo.RemoveItem 0
Next
End If
'If any part of any element from the 'FullSource' array matches the so far typed ComboBox value, then include it in the list for dropdown
For x = 1 To UBound(FullSource)
Typed_Value = "*" & LCase(OriginalValue) & "*"
If LCase(FullSource(x)) Like Typed_Value Then
Me.TempCombo.Object.AddItem FullSource(x)
End If
Next
Me.TempCombo.Value = OriginalValue
Me.TempCombo.ListRows = 12
Me.TempCombo.DropDown
End Select
End Sub
代码似乎可以很好地过滤。但是下拉列表高度只有一个单位高。我必须使用鼠标按钮滚动浏览这个小框。
为什么下拉列表的大小会减小对我来说是个谜,如果能对此有所了解,我将不胜感激。也许我忽略了一些设置。
谢谢
【问题讨论】:
标签: vba excel combobox activexobject