按下向下/向上箭头时如何在不选择项目的情况下使用ComboBox? VBA

时间:2018-07-04 13:44:29

标签: excel vba combobox

我有一个项目列表,一旦用户在组合框中键入,这些列表就会被过滤。因此,如果用户按下向下箭头,则第一个项目被选中,并且列表再次被过滤,因此它仍然是列表中最后一个可用的项目。

我不希望选择这些项目,除非按Enter键。就像鼠标悬停并单击一个项目以将其选中一样。

编辑:我的问题仍然没有得到我们的VBA专家的解答。

EDIT_2:我仍在寻找解决方案。我尝试了不同的方法来实现此目的,但似乎还是有问题。任何人都可以查看我提供的代码。

这是我尝试的代码,但失败了:

Option Explicit
Dim Abort As Boolean
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim xCombox As OLEObject
    Dim xStr As String
    Dim xWs As Worksheet
    Set xWs = Application.ActiveSheet
    On Error Resume Next
    Set xCombox = xWs.OLEObjects("TempCombo")
    With xCombox
        .ListFillRange = ""
        .LinkedCell = ""
        .Visible = False
        .ListRows = "5"
    End With
    If Target.Validation.Type = 3 Then
        Target.Validation.InCellDropdown = False
        Cancel = True
        xStr = Target.Validation.Formula1
        xStr = Right(xStr, Len(xStr) - 1)
        If xStr = "" Then Exit Sub
        With xCombox
            .Visible = True
            .Left = Target.Left
            .Top = Target.Top
            .Width = Target.Width + 5
            .Height = Target.Height + 3
            .ListFillRange = xStr
            .LinkedCell = Target.Address
        End With
        xCombox.Activate
        'Me.TempCombo.DropDown
    End If

End Sub

 Private Sub TempCombo_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Select Case KeyCode


        Case 38  'Up
            If TempCombo.ListIndex <= 0 Then KeyCode = 0 'ignore "Up" press if already on the first selection, or if not on a selection
            Abort = True
            If Not KeyCode = 0 Then ' If on a selection past the first entry
                KeyCode = 0
            'Manually choose next entry, cancel key press
                TempCombo.ListIndex = TempCombo.ListIndex - 1

            End If
            Me.TempCombo.DropDown

        Case 40 'Down
            If TempCombo.ListIndex = TempCombo.ListCount - 1 Then KeyCode = 0
        ' This method was from the discussion I linked, prevents "falling off the bottom of the list"
            Abort = True
            If Not KeyCode = 0 Then ' If on a selection before the last entry
                KeyCode = 0
            'Manually choose next entry, cancel key press
                TempCombo.ListIndex = TempCombo.ListIndex + 1

            End If
            Me.TempCombo.DropDown
    End Select
    Abort = False

End Sub

Private Sub TempCombo_Change()
    If Abort Then Exit Sub ' Stop Event code if flag set
    Abort = True
    ' sets the flag until finished with commands to prevent changes made by code triggering the event multiple times

        Case 9
            Application.ActiveCell.Offset(0, 1).Activate
        Case 13

        'Start
        If Not Intersect(ActiveCell, Range("N8")) Is Nothing Then
        Application.OnKey "~", ActiveSheet.Range("P9").Select
        Application.OnKey "~"
        ElseIf Not Intersect(ActiveCell, Range("P9")) Is Nothing Then
        Application.OnKey "~", ActiveSheet.Range("N11").Select
                Application.OnKey "~"

        End If
        Me.TempCombo.DropDown

    ' ~~~ Insert Code you want to run for other cases here ~~~

    Abort = False
End Sub

0 个答案:

没有答案