【问题标题】:VBA ActiveX dynamic ComboBox reduces ListRows to 1VBA ActiveX 动态组合框将 ListRows 减少到 1
【发布时间】: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


    【解决方案1】:

    您可以使用 Me.TempCombo.Height = 15 来设置高度。

    如果它不起作用,您可能遇到了 ActiveX 控件不稳定问题。请参阅Excel VBA ComboBox DropDown Button Size--changed itself 以使用表单控件而不是 ActiveX。

    Dynamically adjusting the width of a combobox in Excel VBA 了解有关动态设置的更多详细信息。

    【讨论】:

    • 不幸的是这并没有解决问题,也许我不清楚。 ListRows 属性好像重置为 1,虽然我是故意设置为 12,这不是像素的问题。
    猜你喜欢
    • 2019-06-12
    • 1970-01-01
    • 2022-11-11
    • 2017-06-22
    • 1970-01-01
    • 2023-03-09
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多