Search code examples
excelvbacomboboxactivex

How to switch between items of ActiveX Combobox with arrow keys


How to switch between item of ActiveX Combobox with arrow keys? I would like to achieve this behavior generated with mouse hovering over item. So that the blue highlight moves as we press down arrow or up arrow key.

enter image description here

I have a clue that it should be done with the CoboboxName_KeyDown event.

Private Sub TempCombo_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Select Case KeyCode
        Case 38 'up
        Case 40 'down
    End Select
End Sub

Solution

  • Manipulating TempCombo.ListIndex is the answer. Example for up arrow key:

    Case 38  'Up
    TempCombo.ListIndex = TempCombo.ListIndex - 1
    Me.TempCombo.DropDown
    

    Using it requires error handling for first and last item on the list. Here is a hint to solve the problem: https://stackoverflow.com/a/34621918/1903793