Home

VBA Drop Down List

%3CLINGO-SUB%20id%3D%22lingo-sub-837589%22%20slang%3D%22en-US%22%3EVBA%20Drop%20Down%20List%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-837589%22%20slang%3D%22en-US%22%3EHi%20Everyone%2C%3CBR%20%2F%3EI%20developed%20a%20combo%20box%20drop%20down%20list%20and%20added%20a%20code%20that%20works%20fine%20but%20doesn%E2%80%99t%20let%20me%20scroll%20down%20my%20listed%20options%20using%20my%20down%20arrow%20keyboard%20button.%20Is%20there%20a%20code%20to%20use%20the%20down%20arrow%20and%20enter%20button%20on%20the%20drop%20down%20list.%3CBR%20%2F%3E%3CBR%20%2F%3EHere%20is%20the%20code%20I%E2%80%99m%20currently%20using%3A%3CBR%20%2F%3E%3CBR%20%2F%3EPrivate%20Sub%20ComboBox1_Change()%3CBR%20%2F%3EComboBox1.ListFillRange%20%3D%20%22DropDownListv1%22%3CBR%20%2F%3EMe.ComboBox1.DropDown%3CBR%20%2F%3EEnd%20Sub%3CBR%20%2F%3E%3CBR%20%2F%3EHope%20anyone%20can%20help%20me!%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-837589%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-838608%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Drop%20Down%20List%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-838608%22%20slang%3D%22en-US%22%3EI%20expect%20changing%20the%20listfillrange%20causes%20the%20listindex%20to%20be%20reset%2C%20hence%20you%20cannot%20go%20through%20the%20items%20iwith%20the%20keyboard%20as%20the%20list%20keeps%20changing%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-839722%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Drop%20Down%20List%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-839722%22%20slang%3D%22en-US%22%3EI%E2%80%99m%20not%20changing%20the%20list%20range.%20The%20issue%20is%20when%20I%20click%20on%20the%20combo%20box%20and%20try%20to%20scroll%20down%20it%20crashes.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-839754%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Drop%20Down%20List%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-839754%22%20slang%3D%22en-US%22%3EWell%2C%20yes%2C%20the%20code%20that%20runs%20as%20you%20change%20the%20combo%20updates%20the%20listfillrange%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-839873%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Drop%20Down%20List%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-839873%22%20slang%3D%22en-US%22%3EOk%20I%20understand%20what%20you%20mean.%20Yes%20the%20purpose%20of%20my%20combo%20box%20is%20for%20me%20to%20type%20in%20the%20first%20two%20letters%20of%20a%20name%20and%20it%20gives%20me%20options%20to%20choose%20the%20right%20one.%20Like%20doing%20a%20google%20search%20in%20excel%20for%20names.%3CBR%20%2F%3E%3CBR%20%2F%3EWhat%20would%20be%20an%20alternate%20code%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-840416%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Drop%20Down%20List%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-840416%22%20slang%3D%22en-US%22%3ECan%20you%20post%20an%20anonymized%20version%20of%20your%20file%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-840433%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Drop%20Down%20List%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-840433%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403591%22%20target%3D%22_blank%22%3E%40Anggie%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can%20set%20the%20ListFillRange%20utilizing%20an%20event%20called%20GotFocus%20so%20that%20it%20doesn't%20set%20the%20ListFillRange%20each%20time%20you%20type%20in%20the%20ComboBox.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EPrivate%20Sub%20ComboBox1_GotFocus()%0AComboBox1.ListFillRange%20%3D%20%22DropDownListv1%22%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-841807%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Drop%20Down%20List%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-841807%22%20slang%3D%22en-US%22%3EThis%20works%20but%20it%20only%20allows%20me%20to%20select%20the%20first%20name%20on%20my%20list%20and%20not%20let%20me%20keep%20scrolling%20down%20the%20other%20options%20using%20the%20down%20key.%20Unless%20I%20use%20my%20clicker%20to%20select%20the%20other%20names.%3CBR%20%2F%3EWhat%20do%20you%20suggest%20can%20be%20added%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-841913%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Drop%20Down%20List%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-841913%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403591%22%20target%3D%22_blank%22%3E%40Anggie%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAre%20you%20having%20any%20other%20events%20like%20KeyPress%20or%20KeyDown%20etc%3F%3C%2FP%3E%3CP%3EIf%20I%20insert%20a%20dummy%20ComboBox%20and%20have%20the%20Change%20and%20GotFocus%20events%2C%20I%20can%20use%20my%20up%20and%20down%20arrow%20keys%20to%20scroll%20up%20and%20down.%3C%2FP%3E%3CP%3EWhy%20not%20upload%20the%20file%20in%20question%20after%20removing%20the%20sensitive%20data%20if%20any%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-847135%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Drop%20Down%20List%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-847135%22%20slang%3D%22en-US%22%3EHi%2C%20can%20you%20explain%20how%20the%20dummy%20box%20works%3F%20I%20have%20typed%20in%20another%20code%3A%3CBR%20%2F%3E%3CBR%20%2F%3EPrivate%20Sub%20ComboBox1_KeyDown(ByVal%20KeyCode%20As%20MSForms.ReturnInteger%2C%20ByVal%20Shift%20As%20Integer)%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20KeyCode%20%26lt%3B%26gt%3B%2040%20And%20KeyCode%20%26lt%3B%26gt%3B%2038%20Then%3CBR%20%2F%3E%3CBR%20%2F%3EComboBox1.ListFillRange%20%3D%20%22DropDownListv1%22%3CBR%20%2F%3E%3CBR%20%2F%3EMe.ComboBox1.DropDown%3CBR%20%2F%3E%3CBR%20%2F%3EElse%3CBR%20%2F%3E%3CBR%20%2F%3EKeyCode%20%3D%200%3CBR%20%2F%3E%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3E%3CBR%20%2F%3EEnd%20Sub%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-847228%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Drop%20Down%20List%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-847228%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403591%22%20target%3D%22_blank%22%3E%40Anggie%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat%20code%20is%20the%20culprit.%3C%2FP%3E%3CP%3EBasically%20with%20that%20code%2C%20you%20are%20asking%20the%20code%20to%20check%2C%20if%20UP%20arrow%20key%20(KeyCode-38)%20or%20Down%20arrow%20key%20(KeyCode-40)%20is%20pressed%2C%20don't%20do%20anything%20and%20disable%20those%20keys%20when%20you%20say%20KeyCode%20%3D%200%20in%20the%20Else%20clause.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat's%20the%20need%20of%20that%20code%3F%20Why%20are%20you%20having%20same%20code%20with%20multiple%20events%3F%3C%2FP%3E%3CP%3EDelete%20this%20KeyDown%20event%20code%20and%20your%20ComboBox%20will%20start%20working.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-847260%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Drop%20Down%20List%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-847260%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403591%22%20target%3D%22_blank%22%3E%40Anggie%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20not%20sure%20what%20exactly%20you%20are%20trying%20to%20do.%3C%2FP%3E%3CP%3EAll%20I%20can%20suggest%20is%2C%20disable%20all%20the%20codes%20for%20ComboBox1%20and%20have%20only%20one%20code%20which%20should%20be%20as%20below%20to%20set%20the%20ListFillRange%20once%20the%20ComboBox%20gets%20focus.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EPrivate%20Sub%20ComboBox1_GotFocus()%0AComboBox1.ListFillRange%20%3D%20%22DropDownListv1%22%0AEnd%20Sub%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnd%20then%20see%20if%20your%20ComboBox%20starts%20behaving%20normal%20and%20then%20think%20of%20adding%20other%20codes%20for%20ComboBox%20events%20if%20required.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-847251%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Drop%20Down%20List%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-847251%22%20slang%3D%22en-US%22%3EWell%20the%20reason%20I%20added%20the%20keypress%20event%20was%20because%20I%20want%20the%20list%20to%20update%20while%20I%20type%20in%20the%20combo%20box.%20The%20problem%20with%20that%20is%20when%20I%20use%20the%20up%20and%20down%20arrow%20keys%20to%20scroll%20down%20the%20list%20it%20crashes%20and%20I%20would%20lose%20the%20initial%20codes%20I%20used.%20That%20is%20why%20I%20added%20the%20two%20keypress%20events.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-847319%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Drop%20Down%20List%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-847319%22%20slang%3D%22en-US%22%3EThe%20code%20you%20sent%20me%20runs%20smoothly.%20Now%20I%20want%20to%20know%20which%20element%20in%20my%20combo%20box%20has%20the%20focus.%20And%20how%20can%20I%20change%20that%20selection%20or%20move%20that%20focus%20to%20another%20element.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-847346%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Drop%20Down%20List%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-847346%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403591%22%20target%3D%22_blank%22%3E%40Anggie%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENow%20place%20this%20code...%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EPrivate%20Sub%20ComboBox1_Change()%0AMe.ComboBox1.DropDown%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EAnd%20use%20UP%20and%20Down%20arrow%20keys%20to%20scroll%20up%20and%20down%20and%20hit%20Enter%20to%20select%20an%20item.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Anggie
Occasional Contributor
Hi Everyone,
I developed a combo box drop down list and added a code that works fine but doesn’t let me scroll down my listed options using my down arrow keyboard button. Is there a code to use the down arrow and enter button on the drop down list.

Here is the code I’m currently using:

Private Sub ComboBox1_Change()
ComboBox1.ListFillRange = "DropDownListv1"
Me.ComboBox1.DropDown
End Sub

Hope anyone can help me!
14 Replies
I expect changing the listfillrange causes the listindex to be reset, hence you cannot go through the items iwith the keyboard as the list keeps changing?
I’m not changing the list range. The issue is when I click on the combo box and try to scroll down it crashes.
Well, yes, the code that runs as you change the combo updates the listfillrange
Ok I understand what you mean. Yes the purpose of my combo box is for me to type in the first two letters of a name and it gives me options to choose the right one. Like doing a google search in excel for names.

What would be an alternate code?
Can you post an anonymized version of your file?

@Anggie 

You can set the ListFillRange utilizing an event called GotFocus so that it doesn't set the ListFillRange each time you type in the ComboBox.

 

Private Sub ComboBox1_GotFocus()
ComboBox1.ListFillRange = "DropDownListv1"
End Sub

 

This works but it only allows me to select the first name on my list and not let me keep scrolling down the other options using the down key. Unless I use my clicker to select the other names.
What do you suggest can be added?

@Anggie 

Are you having any other events like KeyPress or KeyDown etc?

If I insert a dummy ComboBox and have the Change and GotFocus events, I can use my up and down arrow keys to scroll up and down.

Why not upload the file in question after removing the sensitive data if any?

Hi, can you explain how the dummy box works? I have typed in another code:

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

@Anggie 

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.

Well 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.

@Anggie 

 

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.

The 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.

@Anggie 

Now place this code...

Private Sub ComboBox1_Change()
Me.ComboBox1.DropDown
End Sub

And use UP and Down arrow keys to scroll up and down and hit Enter to select an item.

Related Conversations
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
flashing a white screen while open new tab
cntvertex in Discussions on
13 Replies
Tabs and Dark Mode
cjc2112 in Discussions on
22 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
28 Replies
PacketMon Components are not loading in WAC 1909
HotCakeX in Windows Admin Center on
2 Replies