Forum Discussion
VBA Drop Down List
Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode <> 40 And KeyCode <> 38 Then
ComboBox1.ListFillRange = "DropDownListv1"
Me.ComboBox1.DropDown
Else
KeyCode = 0
End If
End Sub
That code is the culprit.
Basically with that code, you are asking the code to check, if UP arrow key (KeyCode-38) or Down arrow key (KeyCode-40) is pressed, don't do anything and disable those keys when you say KeyCode = 0 in the Else clause.
What's the need of that code? Why are you having same code with multiple events?
Delete this KeyDown event code and your ComboBox will start working.
- AC22JJul 11, 2022Copper ContributorSubodh_Tiwari_sktneer
Excel gives "Compile error: Invalid use of Me keyword" on this code:
Private Sub ComboBox1_Change()
Me.ComboBox1.DropDown
End Sub - Subodh_Tiwari_sktneerSep 10, 2019Silver Contributor
Now place this code...
Private Sub ComboBox1_Change() Me.ComboBox1.DropDown End SubAnd use UP and Down arrow keys to scroll up and down and hit Enter to select an item.
- AnggieSep 10, 2019Copper ContributorThe code you sent me runs smoothly. Now I want to know which element in my combo box has the focus. And how can I change that selection or move that focus to another element.
- Subodh_Tiwari_sktneerSep 10, 2019Silver Contributor
I am not sure what exactly you are trying to do.
All I can suggest is, disable all the codes for ComboBox1 and have only one code which should be as below to set the ListFillRange once the ComboBox gets focus.
Private Sub ComboBox1_GotFocus() ComboBox1.ListFillRange = "DropDownListv1" End Sub
And then see if your ComboBox starts behaving normal and then think of adding other codes for ComboBox events if required.
- AnggieSep 10, 2019Copper ContributorWell the reason I added the keypress event was because I want the list to update while I type in the combo box. The problem with that is when I use the up and down arrow keys to scroll down the list it crashes and I would lose the initial codes I used. That is why I added the two keypress events.