Can I use key press (enter) with Userform listbox

Pisitju

New Member
Joined
Dec 8, 2020
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hi everyone

I have a question about userform listbox?

I want to know about userform listbox can use key press (enter) for select data replace to click button(+)

VBA code below is code of button (+)

Private Sub CommandButton1_Click()
Dim lr As Long, i As Long

lr = Range("AE" & Rows.Count).End(3).Row + 1
If lr < 21 Then lr = 21

With ListBox1
For i = 0 To .ListCount - 1

If ListBox1.Selected(i) = True Then
Range("AE" & lr).Value = .List(i, 0)
Range("AG" & lr).Value = .List(i, 1)
Exit For
End If
Next
End With

Me.TextBox1.Text = ""
Me.TextBox2.Text = ""
TextBox1.SetFocus
End Sub

keypress.png
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Please post code within code tags (use vba button on posting toolbar) to maintain indentation and readability.
I don't understand how your form knows which sheet that AE refers to. Regardless, you could try pasting the code in the listbox KeyUp event. I would put this as the first line to be executed:
VBA Code:
If KeyCode <> 13 Or Me.ListBox1.ListIndex < 0 Then Exit Sub
That way, if any key other than Enter is pressed, or if nothing in the listbox is selected the code should terminate.
 
Upvote 0
Hi Mr.Micron

I trail to run code of you but can not use enter key (nothing happened)

Thank you for coding mr.micron

and Please help me to solve this problem.
 
Upvote 0
Research how to step through code because it is seldom that "nothing happens". Code likely executes but doesn't do what you expect. This shows that if I select a listbox item and press enter, I can show the selected list value (the listbox value) in a message box:
1692415783026.png

You may need to loop through many selected items, and that would be the next step.
 
Upvote 0
Than you mr.micon
I can press enter button

Private Sub ListBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Dim lr As Long, i As Long
lr = Range("AE" & Rows.Count).End(3).Row + 1
If lr < 21 Then lr = 21

With ListBox1
For i = 0 To .ListCount - 1
If ListBox1.Selected(i) = True Then
Range("AE" & lr).Value = .List(i, 0)
Range("AG" & lr).Value = .List(i, 1)
Exit For
End If
Next
End With
Me.TextBox1.Text = ""
Me.TextBox2.Text = ""
TextBox1.SetFocus
(If KeyCode <> 13 Or Me.ListBox1.ListIndex < 0 Then Exit Sub)
End Sub

Thank you for solve your problem
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top